閱讀238 返回首頁    go 阿裏雲 go 技術社區[雲棲]


如何在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 line
collect2: 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 t
clang: 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 in
t.m:5:19: error: expected ‘;’ before ‘{’ token
  @autoreleasepool {
                   ^
但是clang中是沒有問題的,原因仍然未知 :(

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

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



最後更新:2017-04-03 06:03:05

  上一篇:go Mongodb多表查詢
  下一篇:go rake卸載安裝,版本切換