不是很喜欢用android ndk的ndk-build 来对程序进行编译的。需要自己维护一个makefile, 不能和主流IDE 比如eclipse相结合. r5开始ndk有了standalone toolchain, 可以直接用makefile按熟悉的linux的g++的编译配置方式进行编译. 再和eclipse的结合方面虽然手动配置起来是有点麻烦,但是如果通过脚本根据一个模板建立应该是会大大提高工作的效率.
(本文环境是在 ubuntu10.04+eclipse3.7下)
首先, 建立ndk的 arm gcc toolchain
firstly, build the android gcc toolchain
cd <NDK_ROOT>/
sh build/tools/make-standalone-toolchain.sh --platform=android-9 --install-dir=/home/laschweinski/android-9-toolchain
<NDK_ROOT> 是ndk r5+的目录
--install-dir 是standalone android gcc存放的位置. in this case, /home/laschweinski/android-9-toolchain
--platform 用哪个版本,用android-5 应该是最好的,往后的任何版本都是可以用的
必须在<NDK_ROOT> 根目录下输入第二条命令才能成功建立.
具体参见http://www.srombauts.fr/android-ndk-r5b/docs/STANDALONE-TOOLCHAIN.html
或者ndk下的文档.
you could reference http://www.srombauts.fr/android-ndk-r5b/docs/STANDALONE-TOOLCHAIN.html for the details
or the doc in ndk_root
2. 将toolchain 引入到环境变量中.
set the toolchain path to the system PATH variable.
在ubuntu下是:(其它几个平台再补充)
~/.bashrc 目录是决定该用户的环境变量的,
但他是局部的,如果要在eclipse中也能使用这些toolchain 就必须写入到/etc/profile,它使用于所有的shell
在这里 我加了此句在它的最后一行
export PATH="$PATH:/home/laschweinski/android/toolchain/bin"
这时logout以后在终端下输入命令
laschweinski@laschweinski-desktop:~$ arm
arm2hpdl arm-linux-androideabi-gdbtui
arm-linux-androideabi-addr2line arm-linux-androideabi-gprof
arm-linux-androideabi-ar arm-linux-androideabi-ld
arm-linux-androideabi-as arm-linux-androideabi-nm
arm-linux-androideabi-c++ arm-linux-androideabi-objcopy
arm-linux-androideabi-c++filt arm-linux-androideabi-objdump
arm-linux-androideabi-cpp arm-linux-androideabi-ranlib
arm-linux-androideabi-g++ arm-linux-androideabi-readelf
arm-linux-androideabi-gcc arm-linux-androideabi-run
arm-linux-androideabi-gcc-4.4.3 arm-linux-androideabi-size
arm-linux-androideabi-gcov arm-linux-androideabi-strings
arm-linux-androideabi-gdb arm-linux-androideabi-strip
会有这么多命令就是成功了.。
In short, add export PATH="$PATH:[path to your toolchain]/bin" to the final line of /etc/profile
3.在eclipse中配置:
eclispe 3.7 中可以增加除了Debug 和Release 之外的target
右击project-》c/c++ build->setting 窗体头上manage configuration->new 增加新的target
在setting里把GCC C++ Compiler 和linker 的右侧command改成arm-linux-androideabi-g++ GCC C Compiler的右侧command改成arm-linux-androideabi-gcc
另外,ndk都是编译成动态库,在setting-》build artifact中可以把type改为动态链接库或者静态链接库.
静态链接库的话, GCC archiver 那也要把command改为arm-linux-androideabi-ar
这样 编译和链接是所用的就不再是原有的gcc或者g++命令了,而是android 的standalone toolchain了
按正常的eclipse编译流程编译,编译信息差不多是这样:
Building file: ../libjson.cpp
Invoking: GCC C++ Compiler
arm-linux-androideabi-g++ -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"libjson.d" -MT"libjson.d" -o "libjson.o" "../libjson.cpp"
In file included from ../../libjson.h:4,
from ../libjson.cpp:8:
../../Source/JSONDefs.h:157:6: warning: #warning , Release build of libjson, but NDEBUG is not on
Finished building: ../libjson.cpp
Building target: liblibjson.a
Invoking: GCC Archiver
arm-linux-androideabi-ar -r "liblibjson.a" ./JSONAllocator.o ./JSONChildren.o ./JSONDebug.o ./JSONIterators.o ./JSONMemory.o ./JSONNode.o ./JSONNode_Mutex.o ./JSONPreparse.o ./JSONStream.o ./JSONValidator.o ./JSONWorker.o ./JSONWriter.o ./JSON_Base64.o ./internalJSONNode.o ./libjson.o
Finished building target: liblibjson.a
完成后在相应的库文件 可以在文件夹中找到,然后把他复制到android的项目下的libs/armeabi目录下 按正常的android项目跑起来就可以了,
省去很多编写Android.mk的烦扰.也不用再去熟悉其中的那些编译变量了原有的gcc知识足够用了.。
下次介绍下cocos2dx在eclipse上建立项目,用的也是本文的方法
感谢国外开发者lydesik。
Thanks to lydesik, one of contributor to orx