objective-c如何在linux下进入Modern模式

自从apple的obj-c进入2.0后,出现了相对于Legacy模式的Modern模式;Modern模式中出现了一些高级功能(比如ARC),并且出现了一些新的字面语法,新旧模式的差别可以参考apple官方的:
two Platforms 参考文档

在linux中(以下均以ubuntu 15.04为例),默认的clang以及gnustep是不支持Modern模式的;需要清楚的是Modern必须由libobjc2以及libgnustep-base.so同时支持才可以实现。
所以我们必须在系统安装libobjc2和对应的libgnustep-base.so。

  1. 确保系统处于“全新”状态,所谓的“全新”是指没有安装过任何llvm,clang尤其是gnustep的任何文件。如果以前安装过的化必须全部清除干净。/usr/local/lib和/usr/lib中不能残留任何libgnustep*的库
  2. 先密集安装所需的库吧(有些是可选项):
sudo apt-get install cmake
sudo apt-get install build-essential git subversion ninja
sudo apt-get install libffi-dev libxml2-dev libgnutls-dev libicu-dev
sudo apt-get install libblocksruntime-dev libkqueue-dev
sudo apt-get install libpthread-workqueue-dev autoconf libtool
sudo apt-get install curl
sudo apt-get install auto-apt
sudo apt-get install libjpeg
sudo apt-get install libjpeg-dev libtiff-dev libpng12-dev
sudo apt-get install libcups2-dev libfreetype6-dev libcairo2-dev
sudo apt-get install libxt-dev libgl1-mesa-dev
  1. 然后安装clang和llvm
  2. 下载libobjc2(下载地址);编译命令如下:
echo "export CC=clang" >> ~/.bashrc
echo "export CXX=clang++" >> ~/.bashrc
source ~/.bashrc
export CC=clang
export CXX=clang++
cd libobjc2
rm -rf build
mkdir build
cd build
cmake ..
make debug=no
sudo -E make install

实际执行中发现在cmake ..这一步发生错误,错误日志如下:

-- The C compiler identification is Clang 3.5.0 -- The CXX compiler identification is Clang 3.5.0 -- Check for working C compiler: /usr/bin/clang -- Check for working C compiler: /usr/bin/clang -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/clang++ -- Check for working CXX compiler: /usr/bin/clang++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- The ASM compiler identification is Clang -- Found assembler: /usr/bin/clang CMake Error at /usr/share/llvm-3.5/cmake/LLVMConfig.cmake:50 (include):
  include could not find load file:
 /usr/lib/llvm-3.5/share/llvm/cmake/LLVMExports.cmake
Call Stack (most recent call first):
  CMakeLists.txt:129 (find_package)


CMake Error at /usr/share/llvm-3.5/cmake/LLVMConfig.cmake:53 (include):
  include could not find load file:
 /usr/lib/llvm-3.5/share/llvm/cmake/LLVM-Config.cmake
Call Stack (most recent call first):
  CMakeLists.txt:129 (find_package)


-- Untested version of LLVM (3.5.0) found. -- Disabling LLVM options unless explicitly enabled. -- No C++ runtime library found -- Looking for include file pthread.h -- Looking for include file pthread.h - found -- Looking for pthread_create -- Looking for pthread_create - not found -- Looking for pthread_create in pthreads -- Looking for pthread_create in pthreads - not found -- Looking for pthread_create in pthread -- Looking for pthread_create in pthread - found -- Found Threads: TRUE -- GNUstep install type set to LOCAL -- Configuring incomplete, errors occurred! See also "/home/wisy/src/libobjc2/build/CMakeFiles/CMakeOutput.log".
See also "/home/wisy/src/libobjc2/build/CMakeFiles/CMakeError.log".

不需要到CMakeFiles目录中去看CMakeError.log,其实出错原因上面已经说的很清楚了,解决办法却没那么简单,直接做软链接还是会出错,以下是我的解决办法:首先下载llvm:

 svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm

然后执行cmake:

cd llvm
mkdir build
cd build
cmake ..

注意这里后面不需要make,我只要取得cmake生成的一系列*.cmake文件!最终将生成的文件拷贝到指定位置:

cd /usr/lib/llvm-3.5/share/llvm/cmake
sudo cp /home/wisy/src/llvm/build/share/llvm/cmake/* .

再次进入libobjc2/build执行cmake ..,这回没有错误了。
接下来执行安装libobjc2库:

make debug=no
sudo -E make install

5.下载libdispatch并安装:

git clone https://github.com/nickhutchinson/libdispatch.git
export CC=clang
export CXX=clang++
cd libdispatch
./configure
make
sudo -E make install
sudo ldconfig

这时如果用Modern模式编译objc代码会出现警告,而且执行时也会有警告提示:

clang `gnustep-config --objc-flags` -g0 -O3 -s -fobjc-arc -fobjc-nonfragile-abi -fblocks -lobjc -ldispatch -lgnustep-base -lgnustep-gui `gnustep-config --objc-libs` -o 2 2.mclang: warning: argument unused during compilation: '-shared-libgcc'
/usr/bin/ld: warning: libobjc.so.4, needed by /usr/lib/libgnustep-base.so, may conflict with libobjc.so.4.6
/usr/bin/ld: warning: libobjc.so.4, needed by /usr/lib/libgnustep-base.so, may conflict with libobjc.so.4.6
wisy@wisy-X120e:~/src/objc_src$ ./2
Loading two versions of Protocol.  The class that will be used is undefined
Loading two versions of Object.  The class that will be used is undefined
2015-06-11 10:47:20.474 2[21119] (1, hi, 121)

因为libgnustep-base.so还在用老版本的libobjc.so库,所以需要重新编译链接libgnustep-base.so.

6.下载libgnustep-base;这个在configure后,make出错,链接时报大量的符号找不到定义。后来没办法,下载gnustep:

svn co http://svn.gna.org/svn/gnustep/modules/core

然后进入core/scripts目录,执行:

./compile-all

最后在/usr/lib中会出现最新的libgnustep-base.so

如果./compile-all没有生成.so文件则建议依次到base和gui目录中去单独编译。到这里基本上OK了,可以用以下命令行编译:

clang `gnustep-config --objc-flags` -g0 -O3 -s -fobjc-arc -fobjc-nonfragile-abi -fblocks -lobjc -ldispatch -lgnustep-base -lgnustep-gui `gnustep-config --objc-libs` -I/usr/include/GNUstep/ -I/usr/local/include/GNUstep/ -o 9 9.m

可以参考老外写的详细安装日志,但我具体实现下来貌似一些步骤无法实现:

https://feloneouscat.wordpress.com/2013/10/25/objective-c-on-ubuntu-12-04-with-blocks-and-arc/

你可能感兴趣的:(obj-c,arc,Modern)