如何在CentOS7系統上編譯Object-C的項目-源碼安裝篇

源碼安裝相對於yum安裝比較麻煩,就是在不停地遇到錯誤解決錯誤,網站(http://wwwmain.gnustep.org/resources/downloads.php?site=ftp%3A%2F%2Fftp.gnustep.org%2Fpub%2Fgnustep%2F#core)對於依賴的說明也不全面 按照網站說明安裝依賴,依然會缺少一些比較重要的依賴包,例如:xslt、gnutls等。如果不需要依賴的包就可以使用--disable-xx,xx是包名,例如:--disable-gnutls。

第一步、如果要在CentOS系統上編譯Object-C項目,首先必須安裝gcc-objc, 可以通過yum安裝:yum install gcc-objc。

 第二、安裝GNUStep,源碼安裝

如果要了解GNUStep可以參考http://www.gnustep.org/,下載地址http://ftpmain.gnustep.org/pub/gnustep/core/ ,默認安裝目錄是/usr/GNUstep,原安裝指導文檔:http://wiki.gnustep.org/index.php/GNUstep_SVN_Installation_Guide

在安裝之前,根據http://wwwmain.gnustep.org/resources/downloads.php?site=ftp%3A%2F%2Fftp.gnustep.org%2Fpub%2Fgnustep%2F的提示,需要安裝GNUstep的依賴,但是提示的並不全面,可以通過下面的命令把所有依賴的庫都提前安裝:

yum install make libpng libpng-devel libtiff libtiff-devel libobjc libxml2 libxml2-devel libX11-devel libXt-devel libjpeg libjpeg-devel   libxslt libxslt-devel  gnutls gnutls-devel libicu libicu-devel libobjc libffi libffi-devel

通過wget http://ftpmain.gnustep.org/pub/gnustep/core/gnustep-startup-0.32.0.tar.gz 獲得gnustep的源碼

安裝步驟如下:

[root@osboxes ~]# cd ~
[root@osboxes ~]# tar xzvf gnustep-startup-0.32.0.tar.gz
[root@osboxes ~]# cd gnustep-startup-0.32.0/
[root@osboxes ~]# ./InstallGNUstep

安裝成功會顯示:

爲了避免每次登錄都需要運行  . /usr/GNUstep/System/Library/Makefiles/GNUstep.sh  可以執行一下命令。

[root@osboxes ~]# echo '. /usr/GNUstep/System/Library/Makefiles/GNUstep.sh' >> /etc/profile
[root@osboxes ~]# source /etc/profile

如果遇到:錯誤:configure: error: Incomplete support for ffi functionality.

解決方案:可以使用 yum libffi libffi-devel 安裝,也可以根據 readme的提示需要添加:” --ffi=libffi ”, 運行 /InstallGNUstep --ffi=libffi  安裝。

系統依賴的一些包在網站上並沒有全部羅列出來,在安裝的過程中需要根據需要選擇是否使用相關的包,以下是幾個可能遇到的包:

如果遇到:You do not appear to have usable libxslt headers/library.
解決方案:yum install  libxslt libxslt-devel


如果遇到: You do not appear to have usable libgnutls headers/library.
執行 yum install gnutls 缺發現找不到gnutls, 執行 yum -y install epel-release, 然後再執行 yum install gnutls纔可以。
解決方案:yum install gnutls gnutls-devel

如果遇到:You do not appear to have usable ICU headers/libraries. 根據http://site.icu-project.org/,可以知道ICU是一個處理Unicode和全球化的庫

ICU is a mature, widely used set of C/C++ and Java libraries providing Unicode and Globalization support for software applications. ICU is widely portable and gives applications the same results on all platforms and between C/C++ and Java software. 

解決方案:可以簡單一點使用yum安裝:yum install libicu libicu-devel

解決方案:也可以源碼安裝,下載頁面:https://github.com/unicode-org/icu/releases/tag/release-66-1,找到需要下載的版本使用wget下載:wget https://github.com/unicode-org/icu/releases/download/release-66-1/icu4c-66_1-src.tgz。根據編譯的說明也可以執行一下命令編譯:

git clone export https://github.com/unicode-org/icu.git
mkdir icu4c-build
cd icu4c-build
../icu/icu4c/source/runConfigureICU Linux
make check

第三步,安裝 Gorm, Graphical interface builder  (根據需求可選,依賴網頁上描述的是 推薦安裝)

wget http://ftpmain.gnustep.org/pub/gnustep/dev-apps/gorm-1.2.24.tar.gz

tar -zxvf gorm-1.2.24.tar.gz

cd gorm-1.2.24

make install

第四步,安裝 Project Center, Project developer(根據需求可選,依賴網頁描述的是 推薦安裝)

wget http://ftpmain.gnustep.org/pub/gnustep/dev-apps/ProjectCenter-0.6.2.tar.gz

tar -zxvf ProjectCenter-0.6.2.tar.gz

cd cd ProjectCenter-0.6.2

make install

最後,通過以下程序測試環境是否安裝成功,將其保存爲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/GNUstep/System/Library/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

如果遇到:/bin/ld: cannot find -lgnustep-base

需要把gnustep-base的路徑設置一下:

通過 find / -name gnustep-base*  找到 gnustep-base的庫在哪一個路徑下,然後在編譯的時候帶上即可。

gcc `gnustep-config --objc-flags` -Wl,--no-as-needed -lgnustep-base -L/usr/GNUstep/System/Library/Libraries -fconstant-string-class=NSConstantString  -lobjc hello.m -o hello

如果編譯成功,直接執行 ./hello。

 CentOS環境下,Object-C的編譯和執行搞定。

 

參考:

https://www.tutorialspoint.com/objective_c/objective_c_environment_setup.htm

http://wwwmain.gnustep.org/resources/downloads.php?site=ftp%3A%2F%2Fftp.gnustep.org%2Fpub%2Fgnustep%2F

http://ftp.gnustep.org/pub/gnustep/core/

http://gnustep.made-it.com/BuildGuide/index.html#BUILDING.GNUSTEP

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章