# HelloCocoa problem



## retrotron (Feb 6, 2004)

I'm trying to run a simple HelloCocoa class, but I'm running into troubles. For simplicity's sake, I've got the @interface, @implementation, and main( ) in main.m. I compile with 

```
cc main.m -o prog1
```
in the terminal and I get this error:

```
ld: Undefined symbols:
.objc_class_name_Object
.objc_msgSend
```
Can somebody tell me what I'm doing wrong?

Here's the contents of main.m:

```
// a basic class

#import <objc/Object.h>
#import <stdio.h>

// --------- @interface ------------- //

@interface HelloCocoa: Object {
  int someVar;
} 

- (void) print;

@end


// --------- @implementation -------- //

@implementation HelloCocoa;

- (void) print {
  printf("\nHello Cocoa...mmm...yummy.\n");
}

@end


// ---------- try it out ------------ //

int main (int argc, char *argv[]) {

  HelloCocoa *cupOfCocoa;

  cupOfCocoa = [[HelloCocoa alloc] init];

  [cupOfCocoa print];

  [cupOfCocoa free];

  return 0;

} // end main()
```


----------



## Gnomo (Feb 6, 2004)

I'm still really new to programming in objective-c (literally just started yesterday) and haven't touched C or C++ in about 2 years, so please forgive me if I am wrong.

Looking at your code, I see (what looks like) a  syntax error in your main.m file.  Your line "HelloCocoa *cupOfCocoa;"  should probably be "HelloCocoa * cupOfCocoa;"

Or better yet, just combine it with the next line to get "HelloCocoa * cupOfCocoa [[HelloCocoa alloc] init];"

Hope that helps.


----------



## PowerMacX (Feb 6, 2004)

> Looking at your code, I see (what looks like) a syntax error in your main.m file. Your line "HelloCocoa *cupOfCocoa;" should probably be "HelloCocoa * cupOfCocoa;"


  
No, that's not the problem. The * simply means that cupOfCocoa is a pointer to an object of kind HelloCocoa.

The reason you are getting that error is because you need to include ObjC!
Check any Apple example for the right command-line sintaxis (or just use Xcode  )


----------



## retrotron (Feb 7, 2004)

Hmm...thanks for the replies, I just started learning yesterday too.  

I'll try and figure out how to include ObjC...I haven't seen that in any of the examples I'm looking at (the example above is a simplified version -- meaning I've reduced the number of methods and such -- of an example from Kochan's book). In the meantime, if anybody knows exactly what that syntax is, let me know. 

Thanks again.


----------



## anarchie (Feb 7, 2004)

The syntax is:

cc main.m -lobjc -o prog1

If you intend to do more interesting things than subclass Object, you'll end up using the Cocoa framework:

cc main.m -framework Cocoa -o prog1

And, uh, I wouldn't recommend subclassing Object anywhere beyond this mickey mouse program, as it doesn't provide goodies like reference counting and invocations the way NSObject does.


----------



## retrotron (Feb 7, 2004)

Thanks! I'll give it a try! Could you explain what the -lobjc and -framework Cocoa arguments do? I've also seen -framework Foundation...


----------



## havic (Feb 7, 2004)

-lobc is just to include the Objective-C library.  I imagine that apps written in obj-c need it to run 
-framework Cocoa will include the cocoa framework, which is probably also because cocoa applications need it to run.

I only started learning Obj-C yesterday, but the above seems logical, does it not?


----------



## anarchie (Feb 8, 2004)

-lfoo causes the linker to link in the shared library /usr/lib/libfoo.dylib or static archive /usr/lib/libfoo.a.  The dylib takes precedence over the archive, and takes up far less space in the resulting executable.  The library /usr/lib/libSystem.dylib is linked in automatically for C programs; this library is also aliased as libc.dylib.  For C++ programs, /usr/lib/libstdc++.a is also linked automatically.

-framework Foo causes the linker to link in the framework Foo.framework, which can be located in any number of places; usually /System/Library/Frameworks, /Library/Frameworks, and ~/Library/Frameworks.

The Cocoa framework is an 'umbrella' framework, which includes both Foundation and AppKit.  This way, you only need to specify one framework to link.  A framework inevitably itself links to any necessary libraries, and this is why -lobjc is unnecessary when using -framework Cocoa.


----------



## retrotron (Feb 8, 2004)

Thanks so much, that's so helpful to know. You've made my day anarchie. Well, now I'm able to compile my example from the CL, so I guess I'll start trying some Cocoa stuff soon. 

<edit>One more question: when I change the above HelloCocoa to a Cocoa example -- import Foundation/foundation.h, @interface HelloCocoa: NSObject, and NSLog(@"Cocoa is yummy..."); -- and compile it with 'cc -o prog1 -framework Cocoa main.m, I get a bunch of warning messages says 'could not use precompiled header', and it points to a bunch of '/System/Library/Frameworks/Foundation.framework/Headers/someFile.h or someFile-gcc3.p' headers, and it says because they have 'a different date than in precomp'. The program still compiles successfully and works, but I'm juost wondering what these warnings mean and if I should worry about them.</edit>


----------



## anarchie (Feb 8, 2004)

Well, you can run the command 'sudo fixprecomps' to recompile and bring the precompiled headers back up to date.  This might take a while.


----------



## retrotron (Feb 8, 2004)

Did it, and no more precompiler warnings. Compiles perfectly. Thanks so much!


----------



## havic (Feb 10, 2004)

anarchie said:
			
		

> And, uh, I wouldn't recommend subclassing Object anywhere beyond this mickey mouse program, as it doesn't provide goodies like reference counting and invocations the way NSObject does.



I tried porting one of my old command-line apps to Obj-C today, and it goes OK, but you said not to use Object, use NSObject instead.  How?  I tried putting a "NS" in front of the Object bit in the interface declarations, like so:

```
@interface uptime: NSObject
```
but it just said that it could not "find interface declaration for `NSObject', superclass of `uptime'".  How do I use NSObject instead?

Other than that, it runs great.

EDIT:  Another semi-related compiler question:  I'm using PB, and I can't compile my app with it anymore, because it is a "Standard-Tool" but it uses Obj-C.  (I imagine thats why anyway).
I tried adding in "-lobjc -framework cocoa" to the additional compiler flags in the development build style, but no luck.
Does anyone know how to fix this?


----------



## anarchie (Feb 10, 2004)

Er.....did you #import <Cocoa/Cocoa.h> or <Foundation/Foundation.h> ?


----------



## havic (Feb 10, 2004)

Uh...  No, I didn't   

Better try that then


----------

