g++ compile error: undefined reference to a shared library function which exists
Solution 1
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.
Solution 2
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 ..."
dermen
"No problem is insoluble in all conceivable circumstances."
Updated on September 17, 2020Comments
-
dermen over 3 years
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 theH5Fopen
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.
-
dermen about 11 yearsI tried that originally and it gave the same error. There are multiple versions of libhdf5.so* in $HOME/hdf5/lib for instance libhdf5.so.6 and libhdf5.so.6.0.3 etc so I though that being more specific would help, but it didn't.
-
Robert T. McGibbon about 11 yearsAnd what about the -lhdf5? I can reproduce the exact linker error you have in your question on my machine, and adding -lhdf5 solves the problem.
-
dermen about 11 yearsYeah, adding the -lhdf5 didn't help. It is a very strange bug. I installed in exactly the same way on an Ubuntu 10 machine and it worked ok.
-
Mathias711 about 10 yearsStrange. Putting
-lhdf5
before the script, and everything works fine. When I put it after the script, it gives libraries errors. -
johnjg12 about 7 years+500 if i could
-
Ernir Erlingsson over 6 yearsCan confirm that this also fixed the same problem on Windows with Cygwin