如何在linux上构建objective-c程序

               

    swfit目前还是os x独占,以后会不会扩展到其他系统还未可知,但objective-c并不只存在于os x,在linux下gcc和clang都支持obj-c哦,下面简单把如何在ubuntu上构建obj-c做一下说明:


1 安装obj-c或obj-c++(如果需要的话)所需库:

  * build-essential
    * gobjc
    * gobjc++
    * gnustep-devel

 直接用apt-get install 搞定吧 :)


2 用经典的hello world试一下吧:

#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){    @autoreleasepool    {        printf("Hello, World!\n");        NSLog(@"Hello, Objective-C!");            }    return 0;}

wisy@wisy-ThinkPad-X61:~/src/objc_src$ gcc t.m $(gnustep-config --objc-flags) -lgnustep-base -o tgcc/usr/bin/ld: /tmp/cc1azE8Z.o: undefined reference to symbol 'objc_msg_lookup'//usr/lib/x86_64-linux-gnu/libobjc.so.4: error adding symbols: DSO missing from command linecollect2: error: ld returned 1 exit status
神马情况,肿么出错鸟?看出错代码明显是少了链接库文件,度娘搜之,发现少链了 objc 库,加上则通过:

wisy@wisy-ThinkPad-X61:~/src/objc_src$ gcc t.m $(gnustep-config --objc-flags) -lobjc -lgnustep-base -o tgcc

我们再试试clang:

wisy@wisy-ThinkPad-X61:~/src/objc_src$ clang t.m $(gnustep-config --objc-flags) -lobjc -lgnustep-base -o tclang: error: unknown argument: '-fexec-charset=UTF-8'

貌似又不能通过,未知参数啊!那么我们暂且将该参数从列表中去掉再试试吧:首先展开gnustep-config --objc-flags列表,

wisy@wisy-ThinkPad-X61:~/src/objc_src$ gnustep-config --objc-flags-MMD -MP -DGNUSTEP -DGNUSTEP_BASE_LIBRARY=1 -DGNU_GUI_LIBRARY=1 -DGNU_RUNTIME=1 -DGNUSTEP_BASE_LIBRARY=1 -fno-strict-aliasing -fexceptions -fobjc-exceptions -D_NATIVE_OBJC_EXCEPTIONS -pthread -fPIC -Wall -DGSWARN -DGSDIAGNOSE -Wno-import -g -O2 -fgnu-runtime -fconstant-string-class=NSConstantString -fexec-charset=UTF-8 -I. -I/home/wisy/GNUstep/Library/Headers -I/usr/local/include/GNUstep -I/usr/include/GNUstep

然后从中去除上述选项,然后再编译:

wisy@wisy-ThinkPad-X61:~/src/objc_src$ clang t.m -O3 -g0 -MMD -MP -DGNUSTEP -DGNUSTEP_BASE_LIBRARY=1 -DGNU_GUI_LIBRARY=1 -DGNU_RUNTIME=1 -DGNUSTEP_BASE_LIBRARY=1 -fno-strict-aliasing -fexceptions -fobjc-exceptions -D_NATIVE_OBJC_EXCEPTIONS -pthread -fPIC -Wall -DGSWARN -DGSDIAGNOSE -Wno-import -g -O2 -fgnu-runtime -fconstant-string-class=NSConstantString -I. -I/home/wisy/GNUstep/Library/Headers -I/usr/local/include/GNUstep -I/usr/include/GNUstep -lobjc -lgnustep-base -o t

傻办法,不是吗?不过这回没问题啦.我们可以看到用clang生成的可执行文件比gcc生成的将近大一倍,具体什么原因初学的我还不清楚哦.

另外一个要注意的是,gcc和clang虽然都可以编译obj-c代码,但是具体看来可能会有差异,比如以下代码,在gcc中无法编译通过:

@autoreleasepool {  NSLog(@"hello apple! :)"); }

会报如下错误:

wisy@wisy-ThinkPad-X61:~/src/objc_src$ gcc t.m $(gnustep-config --objc-flags) -lobjc -lgnustep-base -o tgcct.m: In function ‘main’:t.m:5:2: error: stray ‘@’ in program  @autoreleasepool {  ^t.m:5:3: error: ‘autoreleasepool’ undeclared (first use in this function)  @autoreleasepool {   ^t.m:5:3: note: each undeclared identifier is reported only once for each function it appears int.m:5:19: error: expected ‘;’ before ‘{’ token  @autoreleasepool {                   ^
但是clang中是没有问题的,原因仍然未知 :(

如果是在os x上用clang编译,可以用以下命令:

clang -fobjc-arc -framework Foundation x.m -o test  



           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/qq_43678568/article/details/86415276