简体   繁体   English

未定义的引用错误 - rand

[英]Undefined reference error - rand

I am creating a command line C++ test application (executable) to be run on my rooted android device. 我正在创建一个命令行C ++测试应用程序(可执行文件),在我的root设备上运行。

The executable uses multiple prebuilt C libraries, one of which use 'rand()'. 可执行文件使用多个预构建的C库,其中一个使用'rand()'。 During linking state i get the error " undefined reference of 'rand' " 在链接状态期间,我得到错误“ 未定义的'rand'参考

To check if the paths are set properly, I have tried below approaches with no success. 要检查路径是否设置正确,我尝试了以下方法但没有成功。

First Approach: I define the rand() in my test source. 第一种方法:我在测试源中定义了rand()。

I notice that i get mutiple definition error, the first definition being in bionic/libc/include/stdlib.h 我注意到我得到了多个定义错误,第一个定义是在bionic / libc / include / stdlib.h中

Second Approach: Used rand() in my test application 第二种方法:在我的测试应用程序中使用rand()

I notice that the linker does not complain about undefined symbol here 我注意到链接器不会在这里抱怨未定义的符号

Third Approach: Unarchived and archived all the object files 第三种方法:取消归档和归档所有目标文件

Found that I still get the undefined reference error. 发现我仍然得到未定义的引用错误。

When the C library that uses 'rand' is compiled into an executable using C test file the mentioned linker error is not seen. 当使用'rand'的C库使用C测试文件编译成可执行文件时,看不到所提到的链接器错误。

I cannot modify the prebuilt static libraries and need to use the library with my C++ based test application. 我不能修改预构建的静态库,需要在我的基于C ++的测试应用程序中使用该库。

Any inputs will be very welcome. 任何投入都将非常受欢迎。

Not enough points to comment so ... 没有足够的评论如此评论......

If you look in bionic's stdlib.h, you will see that rand() is defined as 如果你查看bionic的stdlib.h,你会看到rand()被定义为

static inline int rand(void) { ... } static inline int rand(void){...}

ie the actual code for the function is there in the header file. 即函数的实际代码在头文件中。

This is why your first approach gives you a multiple-definition error. 这就是您的第一种方法为您提供多重定义错误的原因。

You second approach succeeds for the same reason, and (as the function is in-line) generates no 'extern' reference in the object file to cause a search of any libraries. 第二种方法成功的原因相同,并且(因为函数是内联的)在目标文件中不生成'extern'引用以导致搜索任何库。

Next. 下一个。 Your C++ code will be linking against either libc or libstdc++ (you'll need to check). 您的C ++代码将链接到libc或libstdc ++(您需要检查)。 Your prebuilt static library was obviously compiled against a stdlib.h which did not have an inline implementation of rand(). 您的预构建静态库显然是针对stdlib.h编译的,该stdlib.h没有内联实现的rand()。

You need to do an LDD (or readelf) command on the static and see which library its looking for that isn't there on your platform. 您需要在静态上执行LDD(或readelf)命令,并查看它所寻找的库在您的平台上不存在。 It is most likely that the library has the same name as the bionic one, which doesn't export a rand() symbol, and hence the loader is failing to resolve the reference. 该库很可能与仿生库具有相同的名称,它不会导出rand()符号,因此加载器无法解析引用。

Now, as to fixing this ... are you able to at least rebuild the static libs, if not change them? 现在,至于修复这个...你能否至少重建静态库,如果不改变它们? Is your C test file being cross-compiled for android, or natively? 您的C测试文件是否为Android或本机交叉编译?

Use 'android-ndk32-r10-windows-x86_64.zip' , not 'android-ndk64-r10-windows-x86_64.zip' 使用'android-ndk32-r10-windows-x86_64.zip' ,而不是'android-ndk64-r10-windows-x86_64.zip'

APP_ABI := armeabi armeabi-v7a x86

The 3 api is 32bit. 3 api是32位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM