`
lovelease
  • 浏览: 384739 次
社区版块
存档分类
最新评论

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 <stdlib.h>,请自行添加(我是添加过的)。
好了,这个问题应该解决了,在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

分享到:
评论

相关推荐

    cocos2dx 游戏开发系列之三 源码

    在游戏开发领域,cocos2dx是一个广泛使用的开源游戏引擎,尤其在跨平台开发中扮演着重要角色。本篇将深入探讨cocos2dx游戏开发系列的第三部分——源码解析和Android工程的创建与迁移。 首先,cocos2dx是基于C++的,...

    cocos2dx3.x游戏开发之旅电子版

    Cocos2dx是一个开源的游戏开发框架,它基于C++,同时支持多种语言,如Lua和JavaScript,为跨平台游戏开发提供了强大的工具。 在Cocos2dx 3.x版本中,框架进行了诸多优化和改进,提高了性能,简化了API,使得开发者...

    Cocos2dx中UIWebView替换为WKWebView

    在iOS开发中,Cocos2dx是一个广泛使用的游戏开发框架,它允许开发者创建跨平台的2D和3D游戏。然而,随着Apple对App Store审核政策的调整,使用UIWebView的App将不再被接受,因为Apple已经将其列为弃用API。在2020年...

    Cocos2DX.手机游戏开发

    手游开发引擎 Cocos2DX.手机游戏开发

    cocos2dx开发的游戏

    Cocos2d-x是一款强大的开源游戏开发框架,主要...在深入研究这个压缩包时,开发者可以通过阅读源代码理解游戏的工作原理,查看资源文件了解美术设计,分析脚本了解游戏逻辑,从而学习到Cocos2dx的开发技巧和最佳实践。

    cocos2dx-2.x播放mp4

    Cocos2dx是一个基于C++的游戏开发框架,它提供了丰富的功能来帮助开发者创建2D和3D游戏。在这个场景中,我们将深入探讨如何在Cocos2dx 2.x中实现MP4视频的播放。 首先,Cocos2dx 2.x自身并不直接支持MP4格式的视频...

    cocos2dx 工具之CocosBuilder

    Cocos2dx是一个广泛使用的开源游戏开发框架,基于C++,支持跨平台的游戏开发,适用于iOS、Android、Windows等多个平台。在Cocos2dx的生态系统中,有一系列强大的辅助工具,大大提升了开发效率,其中CocosBuilder就是...

    基于cocos2dx的一个小例子

    总的来说,"基于cocos2dx的一个小例子"是一个很好的学习资源,它揭示了Cocos2dx游戏开发的基本流程和关键技术,对于初学者而言,通过这个实例可以深入理解和掌握Cocos2dx框架,为进一步的游戏开发打下坚实基础。

    android项目中嵌入cocos2dx项目demo

    Cocos2dx是一款高效的游戏开发框架,它基于C++,同时提供了Java、Objective-C和Swift的接口,使得开发者可以方便地在Android和iOS平台上进行跨平台开发。下面将详细解释如何将Cocos2dx的HelloWorld工程整合到Android...

    cocos2dx 3.2做的一款赛车游戏

    Cocos2dx是一款流行的开源游戏开发框架,基于C++,支持多平台开发,包括iOS、Android、Windows等。在cocos2dx 3.2版本中制作的赛车游戏,充分利用了这个框架的优势,为玩家提供了流畅的游戏体验和丰富的视觉效果。 ...

    cocos2dx斗地主服务端,客户端在我的资源列表中

    Cocos2dx能够跨平台运行,通过Cocos2dx的iOS绑定,开发者可以使用相同的代码库在iPhone和iPad上部署游戏。 【文件结构】 虽然"Server"是唯一列出的子文件夹,但我们可以假设它包含了服务端的全部代码和资源。通常,...

    cocos2dx2.1.4解决win32中文乱码编译自vs2012

    此代码在cocos2dx2.1.4下面编译有部分错误,我修改后,正常。 下下来后,请在VC++包含目录中链接(F:\cocos2d-x-2.1.4\cocos2dx\platform\win32;F:\cocos2d-x-2.1.4\cocos2dx\platform\third_party\win32\OGLES\GL\;...

    cocos2dx windows环境搭建

    在介绍cocos2dx在Windows环境下的搭建过程中,...以上就是cocos2dx在Windows环境下使用cygwin搭建开发环境的过程和相关知识点。开发者们需要确保安装并正确配置所有工具链和路径,才能顺利进行cocos2dx游戏的开发工作。

    SublimeText启动cocos2dx3.0项目

    Cocos2dx是一款强大的跨平台游戏开发框架,而SublimeText则是一款备受开发者喜爱的文本编辑器,以其高效、轻便和丰富的插件系统著称。将两者结合,可以极大地提升Cocos2dx项目的开发效率。本篇文章将详细介绍如何...

    cocos2dx教程

    学习Cocos2DX不仅可以提高游戏开发效率,还能为跨平台开发打下基础,因为Cocos2DX支持iOS、Android、Windows等多个平台。无论是对个人开发者还是团队,这些教程都是提升技能和项目开发能力的重要资源。

    cocos2dx 2048

    《cocos2dx 2048:游戏开发与cocos2dx框架解析》 “cocos2dx 2048”是一个基于cocos2dx框架开发的流行数字拼图游戏,它仿照了2048游戏的玩法,通过编程技术将2D图形、动画和逻辑控制融合在一起,为玩家提供了趣味且...

    cocos2dx单机大富翁游戏源码

    “cocos2dx单机大富翁游戏源码”是一个基于cocos2dx框架开发的休闲娱乐游戏项目,旨在为开发者提供一个学习和参考的实例,帮助他们理解和掌握cocos2dx引擎在实际游戏开发中的应用。下面,我们将深入探讨cocos2dx框架...

    我所理解的Cocos2dx pdf 下载 地址

    Cocos2dx是一个开源的游戏开发框架,它基于C++,同时支持Lua和JavaScript,让开发者能够快速构建2D和3D游戏。本书旨在帮助读者深入了解计算机图形学、OpenGL ES以及游戏引擎架构,从而提升游戏开发的专业技能。 ...

Global site tag (gtag.js) - Google Analytics