g++编译错误:对一个存在的共享库函数的未定义引用

您所在的位置:网站首页 main未定义 g++编译错误:对一个存在的共享库函数的未定义引用

g++编译错误:对一个存在的共享库函数的未定义引用

2023-04-21 03:01| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

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

其他推荐答案

This is not a bug. See C++ shared library undefined reference to `FooClass::SayHello()'

"Recent versions of GCC reuqire that you put the object files and libraries in the order that they depend on each other ..."

其他推荐答案

You forgot to put -lhdf5 in the compile command. Also, there's no need for -l:$HOME/hdf5/lib/libhdf5.so

This should work: $ g++ -Wl,-rpath,$HOME/hdf5/lib -I$HOME/hdf5/include -L$HOME/hdf5/lib -lhdf5 readHDF5.cpp



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3