I’m learning Objective-C on windows using GNUstep. I have a main class that looks like this:
#import <Foundation/Foundation.h>
#import "Photo.h"
int main(int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
id *photo = [[Photo alloc] init];
[photo setCaption:@"Hello, world!"];
[pool drain];
return 0;
}
However, when I try to compile, it complains about an undefined reference to Photo. If I change Photo.h to Photo.m it works (presumably because there’s an import of Photo.h at the top of Photo.m).
However, this doesn’t seem to be convention. How do I get the linker to see the Photo.h file?
You need to compile Photo.m as well as this main file (e.g.
cc -framework Foundation Photo.m main.m). Merely importing the header is not enough to make GCC compile Photo.m.Incidentally, importing an implementation file is not only unconventional, it won’t compile in most circumstances (specifically, you’ll get duplicate symbols if a file that defines global symbols is included multiple times).