1. 安装gcc和gcc Objective-C包:
[root@localhost ~]# yum install gcc-objc
2. 安装依赖包:
[root@localhost ~]# yum install make libpng libpng-devel libtiff libtiff-devel libobjc libxml2 libxml2-devel libX11-devel libXt-devel libjpeg libjpeg-devel3. 需要安装GNUStep组件,为了支持完整的Objective-C特性,
由于并没有现成的GNUStep组件可供安装,需要下载源码编译并安装。
源码下载地址http://ftpmain.gnustep.org/pub/gnustep/core/ 本人下载的版本是gnustep-startup-0.32.0.tar.gz 默认安装目录是/usr/GNUstep。
[root@localhost ~]# cd ~[root@localhost ~]# tar xzvf gnustep-startup-0.32.0.tar.gz
[root@localhost ~]# cd gnustep-startup-0.32.0/
[root@localhost ~]# ./InstallGNUstep
[root@localhost ~]# echo '. /usr/GNUstep/System/Library/Makefiles/GNUstep.sh' >> /etc/profile
[root@localhost ~]# source /etc/profile
4. 测试环境是否安装成功
新建helloWorld.m文件,加入以下内容:#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"hello world");
[pool drain];
return 0;
}
[root@localhost ~]# gcc `gnustep-config --objc-flags` -L/usr/GNUstep/System/Library/Libraries -lgnustep-base -lobjc helloWorld.m -o helloWorld
[root@localhost ~]# ./helloWorld
2016-05-13 13:55:23.943 helloWorld[24188] hello world
5、可能遇到的问题
如果遇到:错误: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
如果遇到;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的编译和执行搞定。
网友回复