我正在学习Objective-C语言。 由于我没有Mac,我在Ubuntu 11.04平台上编译和运行我的代码。
到目前为止,我正在使用gcc进行编译。 我已经安装了GNUStep,一切正常。 但是后来我开始尝试一些Objective-C 2.0function,比如@property和@synthesize,gcc不允许。
所以我试图用Clang编译代码,但是它似乎没有正确地将我的代码与GNUStep库链接起来,甚至没有使用简单的Hello World程序。
例如,如果我编译下面的代码:
#import <Foundation/Foundation.h> int main(void) { NSLog(@"Hello world!"); return 0; }
编译器的输出是:
/tmp/cc-dHZIp1.o: In function `main': test.m:(.text+0x1f): undefined reference to `NSLog' /tmp/cc-dHZIp1.o: In function `.objc_load_function': test.m:(.text+0x3c): undefined reference to `__objc_exec_class' collect2: ld returned 1 exit status clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
我用来编译的命令是
clang -I /usr/include/GNUstep/ test.m -o test
用-I指令包含GNUStep库(否则,Clang不能findFoundation.h)。
我GOOGLE了我的问题,并访问了GNUStep和Clang网页,但我还没有find解决办法。 所以任何帮助将不胜感激。
谢谢!
问题是库gnustep-base没有被链接器使用。 所以解决方案是使用选项-Xlinker,它将参数发送到clang使用的链接器:
clang -I /usr/include/GNUstep/ -Xlinker -lgnustep-base test.m -o test
声明“-X linker -lgnustep-base”成了魔法。 然而,我在这个与Objective-C中代表一个字符串的类有关的命令时遇到了问题:
./test: Uncaught exception NSInvalidArgumentException, reason: GSFFIInvocation: Class 'NXConstantString'(instance) does not respond to forwardInvocation: for 'hasSuffix:'
我可以解决它添加参数“-fconstant-string-class = NSConstantString”:
clang -I /usr/include/GNUstep/ -fconstant-string-class=NSConstantString \ -Xlinker -lgnustep-base test.m -o test
此外,我已经尝试了一些Objective-C 2.0代码,它似乎工作。
感谢您的帮助!
你可以试试gcc编译器:
首先安装GNU Objective-C Runtime: sudo apt-get install gobjc
然后编译: gcc -o hello hello.m -Wall -lobjc
不要试图重塑GNUstep make,而是使用它:
http://www.gnustep.org/resources/documentation/Developer/Make/Manual/gnustep-make_1.html
另外,您需要从GNUstep安装objc2运行时,并使用此运行时重新安装GNUstep。 这里是Ubuntu的一个脚本,它为你做了一切:
http://wiki.gnustep.org/index.php/GNUstep_under_Ubuntu_Linux