g ++编译错误:对存在的共享库函数的未定义引用 [英] g++ compile error: undefined reference to a shared library function which exists
问题描述
我最近在ubuntu机器上安装了 hdf5 库,现在无法链接到导出的函数。我写了一个简单的测试脚本 readHDF.cpp 来解释问题:
#include $ b int main(int argc,char * argv [])
{
hid_t h5_file_id = H5Fopen(argv [1],H5F_ACC_RDWR,H5P_DEFAULT);
返回0;
}
编译命令是
g ++ -Wl,-rpath,$ HOME / hdf5 / lib -I $ HOME / hdf5 / include \
-L $ HOME / hdf5 / lib -l: $ HOME / hdf5 / lib / libhdf5.so readHDF.cpp
返回以下错误
/tmp/cc6DXdxV.o:函数`main':
readHDF.cpp :(。text + 0x1f):undefined引用'H5check_version'
readHDF.cpp :(。text + 0x3c):对'H5Fopen'的未定义引用
collect2:ld返回1退出状态
我很困惑,因为 nm 命令似乎表示该函数已被导出:
nm -C $ HOME / hdf5 / lib / libhdf5.so | grep H5check_version
返回
0000000000034349 T H5check_version
以及类似的结果 H5Fopen
。任何想法可能会出错?不知道它是否有帮助,但是如果我注释掉脚本中的 H5Fopen
部分,那么它会编译好:
#include< hdf5.h>
$ b int main(int argc,char * argv [])
{
hid_t h5_file_id; // = H5Fopen(argv [1],H5F_ACC_RDWR,H5P_DEFAULT);
返回0;
}
另外,服务器上安装了多个版本的hdf5, python模块(如h5py和tables),但是我无法让它们工作,所以我在我的本地目录中安装了这个版本,并更改了g ++链接器的rpath选项。
好的,解决了。问题在于在编译命令中放置-lhdf5。显然-lhdf5应该放在readHDF.cpp之后。例如 g ++ -Wl,-rpath = $ HOME / hdf5 / lib -L $ HOME / hdf5 / lib -I $ HOME / hdf5 / include readHDF.cpp -lhdf5
will编译没有问题,但 g ++ -Wl,-rpath = $ HOME / hdf5 / lib -L $ HOME / hdf5 / lib -I $ HOME / hdf5 / include -lhdf5 readHDF.cpp
会因未定义的参考错误而失败。有趣的是,这只是Ubuntu 12.04的一个问题,因为两个编译命令都适用于Ubuntu 10.04。
在这篇文章中找到解释的答案:
我想在剧本安全练习后放置-lXXX。
I recently installed the hdf5 library on an ubuntu machine, and am now having trouble linking to the exported functions. I wrote a simple test script readHDF.cpp to explain the issue:
#include <hdf5.h>
int main(int argc, char * argv[])
{
hid_t h5_file_id = H5Fopen(argv[1], H5F_ACC_RDWR, H5P_DEFAULT);
return 0;
}
The compile command is
g++ -Wl,-rpath,$HOME/hdf5/lib -I$HOME/hdf5/include \
-L$HOME/hdf5/lib -l:$HOME/hdf5/lib/libhdf5.so readHDF.cpp
which returns the following error
/tmp/cc6DXdxV.o: In function `main':
readHDF.cpp:(.text+0x1f): undefined reference to `H5check_version'
readHDF.cpp:(.text+0x3c): undefined reference to `H5Fopen'
collect2: ld returned 1 exit status
I am confused because the nm command seems to say that the function has been exported:
nm -C $HOME/hdf5/lib/libhdf5.so | grep H5check_version
which returns
0000000000034349 T H5check_version
and a similar result for H5Fopen
. Any thoughts on what might be going wrong? Not sure if it helps, but if I comment out the H5Fopen
portion of the script, then it compiles fine:
#include <hdf5.h>
int main(int argc, char * argv[])
{
hid_t h5_file_id;// = H5Fopen(argv[1], H5F_ACC_RDWR, H5P_DEFAULT);
return 0;
}
Also there are multiple versions of hdf5 installed on the server which are used by various python modules such as h5py and tables, but I couldn't get any of them to work, so I installed this version in my local directory and changed the rpath options for g++ linker.
Ok, solved. The issue was in the placement of the -lhdf5 in the compile command. Apparently -lhdf5 should be placed after readHDF.cpp. For instance g++ -Wl,-rpath=$HOME/hdf5/lib -L$HOME/hdf5/lib -I$HOME/hdf5/include readHDF.cpp -lhdf5
will compile with no problems, but g++ -Wl,-rpath=$HOME/hdf5/lib -L$HOME/hdf5/lib -I$HOME/hdf5/include -lhdf5 readHDF.cpp
will fail with the undefined reference errors. Interestingly, this was only a problem for Ubuntu 12.04, as both compile commands worked for Ubuntu 10.04.
Found the answer with explanation at this post:
undefined reference to symbol even when nm indicates that this symbol is present
I guess placing -lXXX after the script is safer practice.
这篇关于g ++编译错误:对存在的共享库函数的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!