g++ compile error: undefined reference to a shared library function which exists

前端 未结 3 978
广开言路
广开言路 2021-02-19 05:15

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 re

3条回答
  •  孤独总比滥情好
    2021-02-19 06:17

    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.

提交回复
热议问题