Problems when compiling Objective C with Clang (Ubuntu)

不想你离开。 提交于 2019-12-20 19:43:23

问题


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.

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.

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;
}

The output of the compiler is:

/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)

The command I'm using to compile is

clang -I /usr/include/GNUstep/ test.m -o test

with the -I directive to include the GNUStep libraries (otherwise, Clang is not able to find Foundation.h).

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.

Thanks!


回答1:


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

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:'

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

In addition, I've tried with some Objective-C 2.0 pieces of code and it seems to work.

Thank you for the help!




回答2:


You can try gcc compiler:
First of all install GNU Objective-C Runtime: sudo apt-get install gobjc
then compile: gcc -o hello hello.m -Wall -lobjc




回答3:


You are not able to use ObjC 2.0 features because you're missing a ObjC-runtime supporting those. GCC's runtime is old and outdated, it doesn't support ObjC 2.0. Clang/LLVM doesn't have a acompanied runtime, you need to install the ObjC2-runtime from GNUstep (which can be found here: https://github.com/gnustep/libobjc2 ) and reinstall GNUstep using this runtime.

Here are some bash scripts for different Ubuntu versions, that do everything for you:

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

And please don't try to reinvent GNUstep make, instead, use it:

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

If you really don't think so, here is some excerpt from there:

1.2 Structure of a Makefile

Here is an example makefile (named GNUmakefile to emphasis the fact that it relies on special features of the GNU make program).

#
# 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

This is all that is necessary to define the project.

In your case replace all occurrences of simple with test and you're done

1.3 Running Make

Normally to compile a package which uses the Makefile Package it is purely a matter of typing make from the top-level directory of the package, and the package is compiled without any additional interaction.



来源:https://stackoverflow.com/questions/9337265/problems-when-compiling-objective-c-with-clang-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!