2012-02-18 55 views
10

我正在学习Objective-C语言。由于我没有Mac,我正在Ubuntu 11.04平台中编译和运行我的代码。用Clang编译Objective C时遇到的问题(Ubuntu)

到现在为止,我使用gcc编译。我已经安装了GNUStep,并且一切正常。但后来我开始尝试一些Objective-C 2.0功能,例如@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无法找到Foundation.h)。

我已经GOOGLE了我的问题,并访问了GNUStep和Clang网页,但我还没有找到解决方案。所以任何帮助将不胜感激。

谢谢!

回答

1

你可以试试gcc编译:
首先安装GNU Objective-C运行的:sudo apt-get install gobjc
然后编译:gcc -o hello hello.m -Wall -lobjc

+0

嗨URLArenzo。我已经安装了该软件包,并尝试gcc编译(它工作得很完美)。但是,我发现gcc不支持Objective-C 2.0的某些功能;所以我试图用Clang编译器运行。 – 2012-02-18 16:22:41

6

的问题是,没有被使用的链接库GNUstep的基地。因此,要解决这个用的是选项-Xlinker,发送参数由铛用的链接:

clang -I /usr/include/GNUstep/ -Xlinker -lgnustep-base test.m -o test 

声明“-X连接-lgnustep基地”制造的魔力。然而,我与这个命令相关的类,它表示在Objective-C的字符串问题:

./test: Uncaught exception NSInvalidArgumentException, reason: GSFFIInvocation: 
Class 'NXConstantString'(instance) does not respond to forwardInvocation: for 
'hasSuffix:' 

我能解决它添加参数“-fconstant串级= NSConstantString”:

clang -I /usr/include/GNUstep/ -fconstant-string-class=NSConstantString \ 
-Xlinker -lgnustep-base test.m -o test 

此外,我已经尝试了一些Objective-C 2.0代码,它似乎工作。

谢谢你的帮助!