Cocos2dx开发解决undefined reference to 'atof'和x86平台下报internal compiler error的错误

阅读更多
最近在为游戏做java sdk的cocos2dx引擎层代码时遇到两个问题:
1.游戏安装后无法在老设备(4.4)上运行,6.0没问题。crash信息为:
xxx/proj.android/../cocos2d/cocos/./platform/CCFileUtils.cpp:277: error: undefined reference to 'atof'
xxx/proj.android/../cocos2d/cocos/./platform/CCFileUtils.cpp:286: error: undefined reference to 'atof'
xxx/proj.android/../cocos2d/cocos/./base/CCConsole.cpp:1224: error: undefined reference to 'srand'
xxx/proj.android/../cocos2d/cocos/./base/CCConsole.cpp:1225: error: undefined reference to 'rand'
xxx/proj.android/../cocos2d/cocos/./base/CCConsole.cpp:1253: error: undefined reference to 'srand'
xxx/proj.android/../cocos2d/cocos/./base/CCConsole.cpp:1254: error: undefined reference to 'rand'
xxx/proj.android/../cocos2d/cocos/./base/ccUtils.cpp:254: error: undefined reference to 'atof'
xxx/proj.android/../cocos2d/cocos/./base/ccRandom.h:117: error: undefined reference to 'rand'
xxx/Pickle/proj.android/../cocos2d/cocos/./base/ccRandom.h:117: error: undefined reference to 'rand'
xxx/proj.android/../cocos2d/cocos/./2d/CCActionTiledGrid.cpp:280: error: undefined reference to 'srand'
xxx/proj.android/../cocos2d/cocos/./2d/CCActionTiledGrid.cpp:605: error: undefined reference to 'srand'

有两种表现,有可能是编译.so文件通过,但是在老设备上运行就crash,或者还有就是编译.so文件时直接提示上面的错误信息,无论哪种解决方法都一样,请设置APP_PLATFORM := android-19(或者更早的),因为google从android-21开始将以上那几个函数放到l.cpp文件里面去了,而之前是放在.h文件中的,而ndk和其他sdk不一样,它是向前兼容(Forwards Compatibility)的,也就是老版本兼容新版本,但新版本不兼容老版本,我们在使用jdk或者android sdk编译时总是喜欢用最新的版本编译,但是ndk编译时请使用老版本编译,其实最理想的状态是ndk的APP_PLATFORM应该等于manifest中的minSdkVersion的值,之前我一直使用android-23编译,才导致了这个问题; 
stackoverflow上有人这么解释:
引用

Google have moved some of the C standard library functions like atof() from being inline functions in header files to normal functions. The latest NDKs will default to building a .so that is only compatible with the latest Android devices that have the atof() function in the device's standard C library (libc.so). This means if you run a library on an older device that has an older version of the C library, you will get an error loading the dll as the expected atof() function will not exist.

如果还是不行,可能你需要将上面那些报错的文件都检查一遍,如果哪个文件没有包含头文件#include ,请自行添加(我是添加过的)。
好了,这个问题应该解决了,在armeabi和armeabi-v7a下面正常编译除了.so,并且老设备也正常运行,可是当我编译x86时,失败了,这就是下面要说的第2个问题。

2.在x86平台下编译so文件失败,提示信息如下
android-ndk-r10e/sources/cxx-stl/gnu-libstdc++/4.9/include/bits/stl_vector.h:93:14: internal compiler error: in tree_node_structure_for_code, at tree.c:408

首先要说明一下我的开发和编译环境:MacOS X + clang,并且使用了c++11的一些新特性,比如lambda表达式,只要程序运行到lambda表达式就crash,最后查出来需要将APP_STL改为c++_static就可以了,既能编译x86架构又不会crash:
#Some functions in stdlib.h (atof, srand, etc) used to be static inlined functions 
#before android-21 and start to exist in libc.so from android-21. So to support 
#old devices before Android-21, APP_PLATFORM should be set before android-21,
#otherwise, application will crash on old devices
APP_PLATFORM := android-19
# ===================== Which APP_STL to use START====================================
#about C++ Library Support, see https://developer.android.com/ndk/guides/cpp-support.html#cs
#Why use c++_static, see http://discuss.cocos2d-x.org/t/why-gnustl-static/23780
#(1)GNU STL runtime: This runtime is the GNU Standard C++ Library, (libstdc++-v3). 
#Its shared library file is named libgnustl_shared.so.
#(2)libc++ runtime: This runtime is an Android port of LLVM libc++. Its shared library file is named libc++_shared.so.
#And our dev environment is MacOS X with clang version:Apple LLVM version 7.3.0 (clang-703.0.31)
#And about LLVM libc++, see http://libcxx.llvm.org/, which says:
#libc++ is a 100% complete C++11 implementation on Apple's OS X.
#LLVM and Clang can self host in C++ and C++11 mode with libc++ on Linux.
#libc++ is also a 100% complete C++14 implementation. A list of new features and changes for C++14 can be found here.
# ===================== Which APP_STL to use END====================================
# Instruct to use the static GNU STL implementation
#APP_STL := gnustl_static
APP_STL := c++_static

#Enable C++11. However, pthread, rtti and exceptions aren’t enabled 
APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -std=c++11 -fsigned-char
APP_LDFLAGS := -latomic
#Define this variable as 4.9 to select that version of the GCC compiler.Define this variable as clang to select the Clang compiler, which is the default value for NDK r13 and later.
#see more:https://developer.android.com/ndk/guides/application_mk.html
#NDK_TOOLCHAIN_VERSION := clang

#Use the following command to compile:
#cocos run -p android -m release --ap android-19
APP_ABI := armeabi, armeabi-v7a, x86, arm64-v8a

ifeq ($(NDK_DEBUG),1)
  APP_CPPFLAGS += -DCOCOS2D_DEBUG=1
  APP_OPTIM := debug
else
  APP_CPPFLAGS += -DNDEBUG
  APP_OPTIM := release
endif

你可能感兴趣的:(android,ndk,so)