编译Objective C与Clang时的问题(Ubuntu) [英] Problems when compiling Objective C with Clang (Ubuntu)
问题描述
我在学习Objective-C语言。因为我没有Mac,我在Ubuntu 11.04平台上编译和运行我的代码。
I'm learning Objective-C language. Since I don't have a Mac, I'm compiling and running my code within Ubuntu 11.04 platform.
到目前为止,我使用gcc编译。我安装了GNUStep,一切正常。但是我开始尝试一些Objective-C 2.0的特性,像@property和@synthesize,gcc不允许。
Until now, I was using gcc to compile. I've installed GNUStep and all was working. But then I started to try some Objective-C 2.0 features, like @property and @synthesize, that gcc does not allow.
所以我试图用Clang ,但似乎它没有正确地链接我的代码与GNUStep库,甚至没有一个简单的Hello世界程序。
So I tried to compile the code with Clang, but it seems that it is not correctly linking my code with the GNUStep libraries, not even with a simple Hello world program.
例如,如果我编译以下代码:
For example, if I compile the following code:
#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
< Clang找不到Foundation.h)。
with the -I directive to include the GNUStep libraries (otherwise, Clang is not able to find Foundation.h).
我已经搜索了我的问题,并访问了GNUStep和Clang网页,但我还没有找到解决方案到它。
I've googled my problem, and visited both GNUStep and Clang web pages, but I haven't found a solution to it. So any help will be appreciated.
谢谢!
推荐答案
问题是链接器没有使用库gnustep-base。所以解决方案是使用选项-Xlinker,发送参数到clang使用的链接器:
The problem was that the library gnustep-base was not being used by the linker. So the solution to this was using the option -Xlinker, that sends arguments to the linker used by clang:
clang -I /usr/include/GNUstep/ -Xlinker -lgnustep-base test.m -o test
-X链接器-lgnustep-base使魔法。但是,我有这个命令与代表一个字符串在Objective-C中的类相关的问题:
The statement "-X linker -lgnustep-base" made the magic. However, I had problems with this command related to the class that represents a string in Objective-C:
./test: Uncaught exception NSInvalidArgumentException, reason: GSFFIInvocation:
Class 'NXConstantString'(instance) does not respond to forwardInvocation: for
'hasSuffix:'
我可以解决它添加参数-fconstant-string-class = NSConstantString:
I could solve it adding the argument "-fconstant-string-class=NSConstantString":
clang -I /usr/include/GNUstep/ -fconstant-string-class=NSConstantString \
-Xlinker -lgnustep-base test.m -o test
此外,我已经尝试过一些Objective-C 2.0代码,它似乎工作。
In addition, I've tried with some Objective-C 2.0 pieces of code and it seems to work.
感谢您的帮助!
这篇关于编译Objective C与Clang时的问题(Ubuntu)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!