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).
An undefined reference has nothing to do with missing headers, that would instead tell you that there are undeclared headers.
Now what that would mean is that your Photo.m class is not being compiled, check your project settings and verify that the file is being compiled and linked to your executable, and the error should be gone.
Sidenote: there is no point in including a .m
file, as these contain implementation details (which you can't be interested in) and not headers (which you should be interested in).
You should also check if your Photo.m is enabled for the desired target. Otherwise it is not compiled and the linker is unable to find it.
精彩评论