我试图在终端中使用gcc
运行在Xcode中编写的多个HelloWorld文件。为了简明起见,这是来自 TutorialsPoint 的一个示例,位于名为myStandaloneHelloWorld.m
的独立文件中:
#import <Foundation/Foundation.h>
@interface SampleClass:NSObject
- (void)sampleMethod;
@end
@implementation SampleClass
- (void)sampleMethod{
NSLog(@"Hello, World! \n");
}
@end
int main()
{
/* my first program in Objective-C */
SampleClass *sampleClass = [[SampleClass alloc]init];
[sampleClass sampleMethod];
return 0;
}
在Xcode中编译但在终端中运行gcc myStandaloneHelloWorld.m
时会产生以下错误列表:
Undefined symbols for architecture x86_64:
"_NSLog", referenced from:
-[SampleClass sampleMethod] in myStandaloneHelloWorld-0af75e.o
"_OBJC_CLASS_$_NSObject", referenced from:
_OBJC_CLASS_$_SampleClass in myStandaloneHelloWorld-0af75e.o
"_OBJC_METACLASS_$_NSObject", referenced from:
_OBJC_METACLASS_$_SampleClass in myStandaloneHelloWorld-0af75e.o
"___CFConstantStringClassReference", referenced from:
CFString in myStandaloneHelloWorld-0af75e.o
"__objc_empty_cache", referenced from:
_OBJC_METACLASS_$_SampleClass in myStandaloneHelloWorld-0af75e.o
_OBJC_CLASS_$_SampleClass in myStandaloneHelloWorld-0af75e.o
"_objc_msgSend", referenced from:
_main in myStandaloneHelloWorld-0af75e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我知道还有其他框架和语法(例如在Xcode中运行的语法非常不同的Xcode命令行工具),但这种语法在终端中运行有什么问题?
答案 0 :(得分:2)
您的程序依赖于Foundation框架。就像您必须告诉编译器(或链接器)有关您的程序所需的库一样,您必须告诉它有关框架的信息。
使用:
gcc myStandaloneHelloWorld.m -framework Foundation