未定义引用错误 - 兰特 [英] Undefined reference error - rand

查看:276
本文介绍了未定义引用错误 - 兰特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个命令行C ++测试应用(可执行文件)是我根深蒂固的Andr​​oid设备上运行。

I am creating a command line C++ test application (executable) to be run on my rooted android device.

该可执行文件使用多个prebuilt C库,其中一个使用'兰特()'。在连接状态,我得到的错误兰特的未定义的引用

The executable uses multiple prebuilt C libraries, one of which use 'rand()'. During linking state i get the error "undefined reference of '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.

我注意到,我得到多发定义错误,第一个定义是仿生/ libc中/有/ stdlib.h中

I notice that i get mutiple definition error, the first definition being in bionic/libc/include/stdlib.h

第二种方法:使用兰特()在我的测试应用程序

Second Approach: Used rand() in my test application

我注意到,链接器不抱怨未定义的符号在这里

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.

在使用兰特是用C测试文件所提到的链接器错误编译成可执行的C库是没有看到。

When the C library that uses 'rand' is compiled into an executable using C test file the mentioned linker error is not seen.

我不能修改prebuilt静态库,需要使用库与我的基于C ++的测试应用程序。

I cannot modify the prebuilt static libraries and need to use the library with my C++ based test application.

任何输入将是非常欢迎的。

Any inputs will be very welcome.

推荐答案

不够分置评所以......

Not enough points to comment so ...

如果您在仿生的stdlib.h中看看,你会看到兰特()被定义为

If you look in bionic's stdlib.h, you will see that rand() is defined as

静态在线 INT兰特(无效){...}

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

即。实际code的作用是那里的头文件。

i.e. the actual code for the function is there in the header file.

这就是为什么你的第一种方法为您提供了一个多定义错误。

This is why your first approach gives you a multiple-definition error.

您第二种方法成功出于同样的原因,以及(作为函数是在线)生成目标文件中没有的extern参考以致使搜索任何库

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.

下一步。你的C ++ code会被链接到的libc的两种或的libstdc ++(你需要检查)。您的prebuilt静态库显然是针对其没有兰特直列实施stdlib.h中编译()。

Next. Your C++ code will be linking against either libc or libstdc++ (you'll need to check). Your prebuilt static library was obviously compiled against a stdlib.h which did not have an inline implementation of rand().

您需要做静态的LDD(或readelf)命令,看看哪个库的寻找,是不是有你的平台上。这是最有可能的库具有相同的名称仿生之一,它不导出兰特()符号,因此装载机未能解决的参考。

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. 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.

现在,为解决这个...你至少能够重建静态库,如果不改变呢?是你的C测试文件被交叉编译的Andr​​oid,或者本身?

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?

这篇关于未定义引用错误 - 兰特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆