第一、如果要在CentOS系統上編譯Object-C項目,首先必須安裝gcc-objc, 可以通過yum安裝:yum install gcc-objc。
第二、安裝GNUStep,
yum安裝
根據https://www.cnblogs.com/zenny-chen/p/4080067.html的提示,可以通過apt-get install gnustep和apt-get install gnustep-devel安裝,在CentOS系統下,首先想通過yum安裝,但是,yum list yum list \*step\* 卻找不到gnustep,這裏需要在/etc/yum.repos.d/添加一個epel.repo【可以通過yum install epel-release或者wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm獲得rpm安裝】,然後通過 yum list \*step\*,可以看到雖然項目的名字是GNUstep,但是在yum的列表裏卻是gnustep,
通過命令yum install gnustep-base gnustep-base-devel gnustep-base-doc gnustep-base-libs gnustep-filesystem gnustep-make gnustep-make-doc 安裝gnustep的make、base、libs等。
如果遇到:GPG key retrieval failed: [Errno 14] curl#37 - "Couldn't open file /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7"
可以關掉gpg的key的檢查來繼續完成安裝,關閉的方法就是修改/etc/yum.repos.d/epel.repo,將gpgcheck=1改成gpgcheck=0,就可以安裝成功了。
最後,通過以下程序測試環境是否安裝成功,將其保存爲hello.m
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"hello world");
[pool drain];
return 0;
}
編譯程序之前,必須設置gnustep的環境:
. /usr/share/GNUstep/Makefiles/GNUstep.sh
然後執行
gcc `gnustep-config --objc-flags` -lgnustep-base hello.m -o hello
如果遇到;undefined reference to symbol 'objc_msg_lookup' 需要在編譯的時候加上選項 -lobjc
gcc `gnustep-config --objc-flags` -lgnustep-base -lobjc hello.m -o hello
如果遇到“error: cannot find interface declaration for ‘NXConstantString’”, 則需要在編譯的命令行加上“-fconstant-string-class=NSConstantString”,
gcc `gnustep-config --objc-flags` -Wl,--no-as-needed -lgnustep-base -fconstant-string-class=NSConstantString -lobjc hello.m -o hello
如果編譯成功,直接執行 ./hello。
CentOS環境下,Object-C的編譯和執行搞定。
參考:
http://wiki.gnustep.org/index.php/GNUstep_SVN_Installation_Guide
ftp://ftp.gnustep.org/pub/gnustep/core/
https://www.techotopia.com/index.php/Building_and_Installing_GNUstep_on_Linux
http://wiki.gnustep.org/index.php/Dependencies
http://wiki.gnustep.org/index.php/Platform:Linux
http://gnustep.made-it.com/BuildGuide/index.html#BUILDING.GNUSTEP