用NDK r9版本编译cocos2d x 2.0.4遇到的各个坑

下面详细介绍一下用NDK r9版本编译cocos2d x 2.0.4可能会遇到的各个坑吧。

错误提示1:

Android NDK: WARNING:E:\cocos2d-x\cocos2d-2.0-x-2.0.4/cocos2dx/Android.mk:cocos2dx_static: LOCAL_LDLIBS is always ignored for static libraries   
Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver
Gdbsetup       : libs/armeabi/gdb.setup
"Compile++ thumb : cocosdenshion_static <= SimpleAudioEngine.cpp
E:\cocos2d-x\cocos2d-2.0-x-2.0.4/CocosDenshion/android/SimpleAudioEngine.cpp: In constructor 'CocosDenshion::SimpleAudioEngine::SimpleAudioEngine()':
E:\cocos2d-x\cocos2d-2.0-x-2.0.4/CocosDenshion/android/SimpleAudioEngine.cpp:159:2: error: format not a string literal and no format arguments [-Werror=format-security]
cc1plus.exe: some warnings being treated as errors

make: *** [obj/local/armeabi/objs-debug/cocosdenshion_static/SimpleAudioEngine.o] Error 1

解决方法:把SimpleAudioEngine.cpp第159行注释掉

在文件夹CocosDenshion/android中找到SimpleAudioEngine.cpp文件,定位到第159行,将其注释掉如下:

//LOGD(deviceModel);

再次用Cygwin Terminal软件运行build_native.sh,又出现新的提示错误。 

错误提示2:

E:/cocos2d-2.0-x-2.0.4/cocos2d-2.0-x-2.0.4/testa/proj.android/../../CocosDenshion/android/opensl/OpenSLEngine.cpp:292:45: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
E:/cocos2d-2.0-x-2.0.4/cocos2d-2.0-x-2.0.4/testa/proj.android/../../CocosDenshion/android/opensl/OpenSLEngine.cpp:292:45: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
E:/cocos2d-2.0-x-2.0.4/cocos2d-2.0-x-2.0.4/testa/proj.android/../../CocosDenshion/android/opensl/OpenSLEngine.cpp: In member function 'void OpenSLEngine::create               Engine(void*)':
E:/cocos2d-2.0-x-2.0.4/cocos2d-2.0-x-2.0.4/testa/proj.android/../../CocosDenshion/android/opensl/OpenSLEngine.cpp:324:3: error: format not a string literal and                no format arguments [-Werror=format-security]
cc1plus.exe: some warnings being treated as errors

/cygdrive/e/android-ndk-r9c/build/core/build-binary.mk:388: recipe for target 'obj/local/armeabi/objs-debug/cocosdenshion_static/opensl/OpenSLEngine.o' failed
make: *** [obj/local/armeabi/objs-debug/cocosdenshion_static/opensl/OpenSLEngine.o] Error 1
make: Leaving directory '/cygdrive/e/cocos2d-2.0-x-2.0.4/cocos2d-2.0-x-2.0.4/testa/proj.android'

错误提示解读:warning我们就不管了,先只看error,OpenSLEngine.cpp文件324行error。

解决方法:把cocos2d-2.0-x-2.0.4\CocosDenshion\android\opensl文件夹下的OpenSLEngine.cpp文件

324行注释掉 /*LOGD(errorInfo);*/

再次运行build_native.sh,又提示错误。

错误提示3:

E:/cocos2d-2.0-x-2.0.4/cocos2d-2.0-x-2.0.4/testa/proj.android/../../CocosDenshion/android/opensl/OpenSLEngine.cpp:292:45: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
E:/cocos2d-2.0-x-2.0.4/cocos2d-2.0-x-2.0.4/testa/proj.android/../../CocosDenshion/android/opensl/OpenSLEngine.cpp:292:45: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
[armeabi] Compile++ thumb: cocosdenshion_static <= SimpleAudioEngineOpenSL.cpp
E:/cocos2d-2.0-x-2.0.4/cocos2d-2.0-x-2.0.4/testa/proj.android/../../CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp: In member function 'bool SimpleAudioEngineOpenSL::initEngine()':
E:/cocos2d-2.0-x-2.0.4/cocos2d-2.0-x-2.0.4/testa/proj.android/../../CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp:41:5: error: format not a string literal and no format arguments [-Werror=format-security]
cc1plus.exe: some warnings being treated as errors

/cygdrive/e/android-ndk-r9c/build/core/build-binary.mk:388: recipe for target 'obj/local/armeabi/objs-debug/cocosdens                hion_static/opensl/SimpleAudioEngineOpenSL.o' failed
make: *** [obj/local/armeabi/objs-debug/cocosdenshion_static/opensl/SimpleAudioEngineOpenSL.o] Error 1
make: Leaving directory '/cygdrive/e/cocos2d-2.0-x-2.0.4/cocos2d-2.0-x-2.0.4/testa/proj.android'

这次轮到SimpleAudioEngineOpenSL.cpp文件第41行。

解决方法:把cocos2d-2.0-x-2.0.4\CocosDenshion\android\opensl文件夹下的SimpleAudioEngineOpenSL.cpp文件第41行注释掉:

/* LOGD(errorInfo);*/

再次运行build_native.sh。又提示错误,warning我们就不管了,先只看error。

错误提示4:

/cygdrive/e/android-ndk-r9c/build/core/build-binary.mk:388: recipe for target 'obj/local/armeabi/objs-debug/cocos2d x_static/platform/android/CCCommon.o' failed
make: *** [obj/local/armeabi/objs-debug/cocos2dx_static/platform/android/CCCommon.o] Error 1
make: Leaving directory '/cygdrive/e/cocos2d-2.0-x-2.0.4/cocos2d-2.0-x-2.0.4/testa/proj.android'

解决方法:这次我们在Application.mk文件后面添加下面代码:

APP_CPPFLAGS += -Wno-error=format-security
再次运行build_native.sh,然后一切就顺利了!我把so文件(libgame.so)在eclipse里面导入android项目,并编译成apk文件,测试成功。

用NDK r9版本编译cocos2d x 2.0.4遇到的各个坑

至此,NDK r9版本就可以用build_native.sh编译成so文件(libgame.so),

已经在真机上测试cocos2d x的hello World成功。

如果再有其他报错应该不是NDK r9本身的原因,建议考虑NDK_ROOT是否设置,或者权限的问题,

或者是项目的代码问题(有一些VC++允许通过的,这里会有细微的差异)。

如果第一次尝试做交叉编译的同学,建议不要用自己写的Classes代码编译,

可以考虑用官方的HelloWorld,这样可以减少困扰。