使用GNU 编译OBjectC
来源:互联网 发布:数据库编辑器有哪些 编辑:程序博客网 时间:2024/06/05 08:16
Compile Objective-C Programs Using gcc
Probably 99% of all Objective-C programmers out there are compiling their programs with XCode very happily. However that doesn't stop the other 1%, who are brave enough or simply don't have choice, from compiling Objective-C programs using gcc under command line.
Being one of that 1%, I had hard time trying compiling my program. I finally had my first program compiled after days of struggling due to lack of information and experience. I'd like to share the lesson I learned so that maybe someone sees this and save a little effort. The process of compiling a very simple objective-c program on several platforms are explained below.
Here is the objective-c program file I used - hello.m (download).
1. To Compile Objective-C Programs on Mac OS X
Compiling on Mac OS X is the simplest, just cd to the directory where hello.m resides and type in the following command.
The compile goes happily and produces an executable file "hello".
2. To Compile Objective-C Programs on Linux
Well, to compile Objective-C programs, GNUstep needs to be installed. Please visit www.gnustep.org to find out how to install GNUstep on your Linux version; or use "apt-get" or "synaptic" if you are running Debian-based distributions (Ubuntu for example).
Once GNUstep is installed, assuming its installation directory is /usr/lib/GNUstep, the following command compiles hello.m.
The compiler may complain about NXConstantString not declared if the last switch "-fconstant-string-class=NSConstantString" is not included.
This process was tested on Ubuntu 7.10 and should work for other linux distributions as well.
Thanks to commenter digreamon, I updated the above command with -I `gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS` and -L `gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES`. It saves you the trouble to manually locate the locations of headers and libraries. I have not verified this on Windows, but they might work too.
Also note that if you did not include -D_NATIVE_OBJC_EXCEPTIONS, you may run into the following error:
/usr/include/GNUstep/Foundation/NSException.h:42:2: error: #error The current setting for native-objc-exceptions does not match that of gnustep-base ... please correct this.
3. To Compile Objective-C Programs on Windows
This is the most tricky one. In order to compile, GNUstep for Windows needs to be installed first. Point your browser to http://www.gnustep.org/experience/Windows.html and download the three necessary packages: GNUstep MSYS System, GNUstep Core and GNUStep Devel. Install them in the order as they were mentioned.
Once everything is setup, use the following command to compile your hello.m program. The compiler emits some linking information and that means compilation is successful.
Be very careful about the -lobjc and -lgnustep-base switch, they must appear after file name "hello.m", otherwise linking will fail.
I tried to compile with the following command, and very weird linking errors came up:
$ gcc -o hello -I /GNUstep/System/Library/Headers \
-L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base hello.m
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0xe): undefined reference to `NSLog'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x47): undefined reference to `objc_get_class'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x59): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x78): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x9b): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0xbb): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0xdf): undefined reference to `__objc_exec_class'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.data+0xfc): undefined reference to `__objc_class_name_NXConstantString'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.data+0x104): undefined reference to `__objc_class_name_NSObject'
collect2: ld returned 1 exit status
make: *** [build] Error 1It took me a few days to realized that the switches need to be following hello.m. I'm still not able to understand the reason behind, though. If anybody knows, please be generous enough to put down a comment.
Being one of that 1%, I had hard time trying compiling my program. I finally had my first program compiled after days of struggling due to lack of information and experience. I'd like to share the lesson I learned so that maybe someone sees this and save a little effort. The process of compiling a very simple objective-c program on several platforms are explained below.
Here is the objective-c program file I used - hello.m (download).
/***************** hello.m *********************/#import <Foundation/Foundation.h>@interface HelloWorld : NSObject- (void) hello;@end@implementation HelloWorld- (void) hello { NSLog(@"hello world!");}@endint main(void) { HelloWorld *hw = [[HelloWorld alloc] init]; [hw hello]; [hw release];}/******************* end ***********************/
1. To Compile Objective-C Programs on Mac OS X
Compiling on Mac OS X is the simplest, just cd to the directory where hello.m resides and type in the following command.
$ gcc -o hello hello.m -framework Foundation
The compile goes happily and produces an executable file "hello".
$ ./hello
2008-01-26 23:10:32.983 hello[381:10b] hello world!
2008-01-26 23:10:32.983 hello[381:10b] hello world!
2. To Compile Objective-C Programs on Linux
Well, to compile Objective-C programs, GNUstep needs to be installed. Please visit www.gnustep.org to find out how to install GNUstep on your Linux version; or use "apt-get" or "synaptic" if you are running Debian-based distributions (Ubuntu for example).
Once GNUstep is installed, assuming its installation directory is /usr/lib/GNUstep, the following command compiles hello.m.
$ gcc -o hello hello.m \
-I `gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS` \
-L `gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES` \
-lgnustep-base -fconstant-string-class=NSConstantString \
-D_NATIVE_OBJC_EXCEPTIONS
-I `gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS` \
-L `gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES` \
-lgnustep-base -fconstant-string-class=NSConstantString \
-D_NATIVE_OBJC_EXCEPTIONS
$ ./hello
2008-01-27 00:02:17.321 hello[7067] hello world!
2008-01-27 00:02:17.321 hello[7067] hello world!
The compiler may complain about NXConstantString not declared if the last switch "-fconstant-string-class=NSConstantString" is not included.
This process was tested on Ubuntu 7.10 and should work for other linux distributions as well.
Thanks to commenter digreamon, I updated the above command with -I `gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS` and -L `gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES`. It saves you the trouble to manually locate the locations of headers and libraries. I have not verified this on Windows, but they might work too.
Also note that if you did not include -D_NATIVE_OBJC_EXCEPTIONS, you may run into the following error:
/usr/include/GNUstep/Foundation/NSException.h:42:2: error: #error The current setting for native-objc-exceptions does not match that of gnustep-base ... please correct this.
3. To Compile Objective-C Programs on Windows
This is the most tricky one. In order to compile, GNUstep for Windows needs to be installed first. Point your browser to http://www.gnustep.org/experience/Windows.html and download the three necessary packages: GNUstep MSYS System, GNUstep Core and GNUStep Devel. Install them in the order as they were mentioned.
Once everything is setup, use the following command to compile your hello.m program. The compiler emits some linking information and that means compilation is successful.
$ gcc -o hello hello.m -I /GNUstep/System/Library/Headers \
-L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base
Info: resolving ___objc_class_name_NXConstantString by linking to __imp____objc_class_name_NXConstantString (auto-import)
Info: resolving ___objc_class_name_NSObject by linking to __imp____objc_class_name_NSObject (auto-import)
$ ./hello
2008-01-27 00:27:14.630 hello[3724] hello world!
-L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base
Info: resolving ___objc_class_name_NXConstantString by linking to __imp____objc_class_name_NXConstantString (auto-import)
Info: resolving ___objc_class_name_NSObject by linking to __imp____objc_class_name_NSObject (auto-import)
$ ./hello
2008-01-27 00:27:14.630 hello[3724] hello world!
Be very careful about the -lobjc and -lgnustep-base switch, they must appear after file name "hello.m", otherwise linking will fail.
I tried to compile with the following command, and very weird linking errors came up:
$ gcc -o hello -I /GNUstep/System/Library/Headers \
-L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base hello.m
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0xe): undefined reference to `NSLog'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x47): undefined reference to `objc_get_class'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x59): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x78): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x9b): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0xbb): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0xdf): undefined reference to `__objc_exec_class'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.data+0xfc): undefined reference to `__objc_class_name_NXConstantString'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.data+0x104): undefined reference to `__objc_class_name_NSObject'
collect2: ld returned 1 exit status
make: *** [build] Error 1
UPDATE
According to commenter "teo", people who still encounter errors with Windows+GNUStep may use the following command. I have not personally verified this, but I assume it does help according to some other commenters feedback.
gcc `gnustep-config --objc-flags` -o hello2 hello2.m \
-L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base
Note that the ` symbol is not the same as a single quote ( ' ).-L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base
Now, happy compiling your first Objective-C program on whichever environment you are in.
出现一些错误:cannot find interface declaration for 'NXConstantString'
$ gcc -o hello hello.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Li
brary/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString
- 使用GNU 编译OBjectC
- 1-objectc-使用对象
- ObjectC----分类的使用
- 使用GNU Autotools 自动编译项目
- 使用GNU make 编译C程序
- flex远程Objectc封装使用
- ubuntu下编译objectc 与object c++
- ubuntu下编译objectc 与object c++
- compile source as objectC++ 后,编译错误
- Windows下使用VC++编译GNU科学计算库 GNU Scientific Library(GSL)
- Windows下使用VC++编译GNU科学计算库 GNU Scientific Library(GSL)
- 【IPHONE开发-OBJECTC入门学习】NSUserDefaults使用
- GNU C++程序编译
- GNU编译工具
- Linux GNU 编译基础知识
- 编译GNU make
- GNU-GCC编译选项
- Object-C-Ubuntu搭配ObjectC编译环…
- 字符串匹配问题:KMP算法
- 十道大数据的题
- c++有关编译期和运行期的访问
- JBoss 系列三:使用4种方式部署应用到JBoss7/WildFly
- 十七道海量数据面试题
- 使用GNU 编译OBjectC
- hibernate之HQL之参数绑定
- Ringo.js嵌入集成(二):使用Listener Filter替换官方的JsgiServlet实现
- OpenCL 和 CUDA 之间的区别
- Windows下的Objective-C集成开发环境(IDE)的搭建
- Crazy Tea Party
- CF204 Div 1 题解
- 徐海蛟:svm理论与实验之1
- hibernate之HQL之引用查询