我正在创建一个小程序,它包含我从桌面上运行的几个源文件(我不使用Xcode),它是从命令行编译的。由于一切都非常小,我想跳过使用头文件,并在我的.m文件中的一切。它想是这样的:Objective-C中不使用头文件
Foo.m
#import <Foundation/Foundation.h>
@interface FooClass : NSObject {
}
- (void) fooFunction;
@end
@implementation FooClass
- (void) fooFunction {
NSLog(@"Printing bar");
}
@end
Main.m
#include <stdlib.h>
#import "FooFunction.m"
int main (int argc, const char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"Running....");
FooFunction *foo = [[FooFunction alloc] init];
[foo fooFunction];
[pool drain];
return 0;
}
当我尝试编译此我得到ld duplicate symbol fooFunction blahblahblah for architecture x86_64
的任何想法,我做错了什么?
那么解决方案是将接口代码放在Main.m文件中?我很感谢你的解释,但我仍然不清楚如何避免使用头文件。 – 2012-02-11 09:58:12
你可以这样做 - 我更新了我的答案来讨论这个问题。 – 2012-02-11 10:01:37
@Eric:避免头文件意味着只有一个名为main.m的文件。但是,如果您不包含任何头文件,那么您将无法使用任何库/ API或组织代码。如果你有多个源文件,你应该使用头文件。如果你有一个main.m,那就完全没有必要。 – 2012-02-11 10:08:50