【问题标题】:Problems when compiling Objective C with Clang (Ubuntu)使用 Clang (Ubuntu) 编译 Objective C 时的问题
【发布时间】:2012-03-09 09:14:19
【问题描述】:

我正在学习 Objective-C 语言。由于我没有 Mac,我在 Ubuntu 11.04 平台上编译和运行我的代码。

到目前为止,我一直在使用 gcc 进行编译。我已经安装了 GNUStep,一切正常。但后来我开始尝试一些 gcc 不允许的 Objective-C 2.0 特性,例如 @property 和 @synthesize。

所以我尝试用 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)。

我搜索了我的问题,并访问了 GNUStep 和 Clang 网页,但我还没有找到解决方案。因此,我们将不胜感激。

谢谢!

【问题讨论】:

    标签: objective-c ubuntu compiler-construction linker clang


    【解决方案1】:

    问题是链接器没有使用库 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 的代码,它似乎可以工作。

    感谢您的帮助!

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 你好 URLArenzo。我已经安装了该软件包,并尝试使用 gcc 进行编译(它运行良好)。但是,我发现 gcc 不允许 Objective-C 2.0 的某些功能;所以我正在尝试使用 Clang 编译器运行。
      【解决方案3】:

      您无法使用 ObjC 2.0 功能,因为您缺少支持这些功能的 ObjC 运行时。 GCC 的运行时陈旧过时,它不支持 ObjC 2.0。 Clang/LLVM 没有附带的运行时,您需要从 GNUstep 安装 ObjC2 运行时(可以在此处找到:https://github.com/gnustep/libobjc2)并使用此运行时重新安装 GNUstep。

      这里有一些适用于不同 Ubuntu 版本的 bash 脚本,它们可以为您完成所有工作:

      http://wiki.gnustep.org/index.php/GNUstep_under_Ubuntu_Linux

      请不要尝试重新发明 GNUstep make,而是使用它:

      http://www.gnustep.org/resources/documentation/Developer/Make/Manual/gnustep-make_1.html

      如果你真的不这么认为,这里有一些摘录:

      1.2 Makefile 的结构

      这是一个示例 makefile(命名为 GNUmakefile 是为了强调它依赖于 GNU make 程序的特殊功能)。

      #
      # An example GNUmakefile
      #
      
      # Include the common variables defined by the Makefile Package
      include $(GNUSTEP_MAKEFILES)/common.make
      
      # Build a simple Objective-C program
      TOOL_NAME = simple
      
      # The Objective-C files to compile
      simple_OBJC_FILES = simple.m
      
      -include GNUmakefile.preamble
      
      # Include in the rules for making GNUstep command-line programs
      include $(GNUSTEP_MAKEFILES)/tool.make
      
      -include GNUmakefile.postamble
      

      这就是定义项目所需的全部内容。

      在您的情况下,将所有出现的simple 替换为test,就完成了

      1.3 运行 Make

      通常编译一个使用Makefile包的包,只需要在包的顶层目录中输入make,包就编译好了,不需要任何额外的交互。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-15
        • 2020-07-21
        • 2012-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多