CCConsole.cpp:738: error: undefined reference to 'rand'

Hey guys,
I have a problem. I’m making an android game. I builded my cocos2dx project for android using as target android-22… But, the application crashes with the error:

java.lang.UnsatisfiedLinkError: Cannot load library: reloc_library[1306]:  2370 cannot locate 'rand'...

I searched on the forums and found that you should build your project with android-20 or less as target if you want to support older devices.

This is what I did. I changed the target to android-19 in project.properties (for my project & for cocos). But when I try to compile the project I get the following errors:

So why wouldn’t cocos2d compile for android-19? And is there something I can do to solve this problem?

This might be related to the problems a lot of people have with AndroidNDK. Depending on what version you have (and the constellation of the stars), your ndk support or does not support rand.

And atof seems to be even worse, since that doesn’t seem to be supported at all. Eventually I will need to search for a solution myself… Havn’t tried out strtof yet though.

Same issue!

failed: Cannot load library: soinfo_relocate(linker.cpp:976): cannot locate symbol "srand" referenced by "libcocos2dcpp.so"...

After setting Android SDK to 16 i get:

proj.android/../cocos2d/cocos/./base/CCConsole.cpp:737: error: undefined             reference to 'srand'
proj.android/../cocos2d/cocos/./base/CCConsole.cpp:738: error: undefined reference to 'rand'
proj.android/../cocos2d/cocos/./base/CCConsole.cpp:765: error: undefined reference to 'srand'
proj.android/../cocos2d/cocos/./base/CCConsole.cpp:766: error: undefined reference to 'rand'
proj.android/../cocos2d/cocos/./base/ccUtils.cpp:205: error: undefined reference to 'atof'
proj.android/../cocos2d/cocos/./base/ccRandom.h:117: error: undefined reference     to 'rand'
proj.android/../cocos2d/cocos/./2d/CCActionTiledGrid.cpp:96: error: undefined reference to 'rand'
proj.android/../cocos2d/cocos/./2d/CCActionTiledGrid.cpp:304: error: undefined reference to 'srand'
proj.android/../cocos2d/cocos/./2d/CCActionTiledGrid.cpp:670: error: undefined reference to 'srand'

Just build cocos and your game with and SDK version prior to 19… change the target in the Android.mk files in both your project and the cocos project…

I have a similar issue as well.
I initially compiled my project for android 22 and it worked fine.
Now that i want to extend the app to even lower devices I’ve changed the target to 19 and I’m getting this

[...] [armeabi] SharedLibrary : libcocos2dcpp.so C:/cocos2d-x-3.9/projects/TDProject/proj.android-studio/../cocos2d/cocos/./base/CCConsole.cpp:737: error: undefined reference to 'srand' C:/cocos2d-x-3.9/projects/TDProject/proj.android-studio/../cocos2d/cocos/./base/CCConsole.cpp:738: error: undefined reference to 'rand' C:/cocos2d-x-3.9/projects/TDProject/proj.android-studio/../cocos2d/cocos/./base/CCConsole.cpp:765: error: undefined reference to 'srand' C:/cocos2d-x-3.9/projects/TDProject/proj.android-studio/../cocos2d/cocos/./base/CCConsole.cpp:766: error: undefined reference to 'rand' C:/cocos2d-x-3.9/projects/TDProject/proj.android-studio/../cocos2d/cocos/./base/ccUtils.cpp:205: error: undefined reference to 'atof' C:/cocos2d-x-3.9/projects/TDProject/proj.android-studio/../cocos2d/cocos/./base/ccRandom.h:117: error: undefined reference to 'rand' C:/cocos2d-x-3.9/projects/TDProject/proj.android-studio/../cocos2d/cocos/./2d/CCActionTiledGrid.cpp:96: error: undefined reference to 'rand' C:/cocos2d-x-3.9/projects/TDProject/proj.android-studio/../cocos2d/cocos/./2d/CCActionTiledGrid.cpp:304: error: undefined reference to 'srand' C:/cocos2d-x-3.9/projects/TDProject/proj.android-studio/../cocos2d/cocos/./2d/CCActionTiledGrid.cpp:670: error: undefined reference to 'srand' collect2.exe: error: ld returned 1 exit status make.exe: *** [obj/local/armeabi/libcocos2dcpp.so] Error 1 make.exe: Leaving directory `C:/cocos2d-x-3.9/projects/TDProject/proj.android-studio/app' Error running command, return code: 2.
Is there a way to solve this problem ?

I changed the target to 20 and it compiled ok. I wonder if there’s a way to compile projects to level 16 to gather more audience?

I have the same problem. Initially working with API-23 and now I would like to test support for older phones. I’m trying to target API-19 and later. SO I change my --ap to android-19 while compiling and get all of the rand/srand/atof errors above. Is there any solution to this?

Could you clarify what you mean by this? If this solved your problem it should solve mine too. A little more detail on the steps you followed would be much appreciated.

The target API for the cocos project has also to be 19… So change the build target for the cocos projet to 19, and it should work.

I have just tried to do this and unfortunately the errors are still the same. Could you provide the file paths for the properties files you have been editing? It may also be important to note I’m compiling for android studio. My command line argument that produces the errors above is “cocos compile -p android --ap android-19 --android-studio”

I had the same problem. Finally, deleting the project and creating new one and compiling again solved my problem.

1 Like

I am having the issue but re-creating the project does not work in release mode.

I can reproduce the issue with the default project.

rand,srand,atof,这几个函数报错的文件检查一下,如果某个文件没有#include “stdlib.h”,就加进去,之后就可以使用android-19编译通过,并且在4.X的系统下运行也没问题。
我只是做安卓的,不懂cocos开发,最近的一个项目总是在5.0以下的设备crash,6.0正常,并且用android-23编译so可以,用android-19编译就报上面的错,调查了半天才找到这个解决方案,供各位参考,其实我也不是很明白cocos-sdk怎么会有这样的问题。感觉和ndk版本有关。

std::rand()
std::srand()
std::atof()