undefined reference with shared library using cmake
Don't assume that the directory containing CMakeLists.txt is the current directory at runtime. Instead, use ${CMAKE_CURRENT_SOURCE_DIR}
whenever you want a path relative to the CMakeLists.txt. For example:
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/hdr)

jrk0414
Updated on June 22, 2022Comments
-
jrk0414 11 months
I have looked a lot of placed to find this answer, but I have not been able to find anything that pertains to my situation. It seems so easy, which is why this is so frustrating.
I am building a project with CMAKE. I am generating two shared libraries. One includes the other. I am also generating an executable. The executable is linking to the shared library that encapsulates the other one. Here is the relevant portion of the code:
################################################################################ # Make the shared libraries ################################################################################ # Standard stuff add_library(er SHARED src/std_math.cc src/snapshot.cc) include_directories(hdr) target_link_libraries(er rt) # DSP library add_library(dsp SHARED src/dsp.cc) include_directories(hdr) target_link_libraries(dsp er /usr/lib/libfftw3f.so /usr/lib/libfftw3.so) ################################################################################ # Make an Executable ################################################################################ message("-- Making executable for testing --") add_executable(er_test test/dsp_test.cc) include_directories(hdr) target_link_libraries(er_test dsp) ################################################################################ # What to do with make install ################################################################################ message ("-- Writting install scripts --") # See if there is an install directory already assigned. If not, set it to the # system default. if (NOT DEFINED INSTALL_DIR) set (INSTALL_DIR /usr/local/) endif (NOT DEFINED INSTALL_DIR) message (" -- install_dir = ${INSTALL_DIR}") # Install the libraries and header files to the appropriate places install (TARGETS er dsp DESTINATION ${INSTALL_DIR}/bin) install (FILES hdr/dsp.hh hdr/snapshot.hh hdr/std_math.hh DESTINATION ${INSTALL_DIR}/include)
Here is the error I get.
CMakeFiles/er_test.dir/test/dsp_test.cc.o: In function `main': dsp_test.cc:(.text.startup+0x11e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <int>(std::basic_ostream<char, std::char_traits<char> >&, std::vector<int, std::allocator<int> > const&)' dsp_test.cc:(.text.startup+0x142): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <int>(std::basic_ostream<char, std::char_traits<char> >&, std::vector<int, std::allocator<int> > const&)' dsp_test.cc:(.text.startup+0x166): undefined reference to `std::vector<int, std::allocator<int> > subvec<int>(std::vector<int, std::allocator<int> > const&, int, int, int)' dsp_test.cc:(.text.startup+0x182): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <int>(std::basic_ostream<char, std::char_traits<char> >&, std::vector<int, std::allocator<int> > const&)' dsp_test.cc:(.text.startup+0x1b6): undefined reference to `std::vector<int, std::allocator<int> > subvec<int>(std::vector<int, std::allocator<int> > const&, int, int, int)'
and there are many more where those came from. I have tried using static libraries, but then it just delays the problem until the next executable I try to include these libraries into. I have also tried using g++ instead of c++. I've tried swapping around library orders. Also, it is not a template issue since at least one of the references it can't find is not a templated function. I have searched the libraries for the symbols, and I was able to find them, although they were prepended and postpended by random characters.
I don't understand what's going on. Please help.
Thanks,
UPDATE
I found another link, specifically this one, that mentioned potential problems with add_subdirectory. This is a subproject to another project. So I sent into the project and built there. It worked! It still doesn't work in the parent directory though. Maybe that can give clues to someone.
Thanks again,
-
jrk0414 almost 9 yearsAmazingly, that fixed it. I would never have thought that would be the problem. Thanks for the help.
-
ostrokach over 6 yearsI was banging my head over an
apbs: symbol lookup error: [...] undefined symbol: X
error. Turns out it was caused by aninclude_directories('/src')
in the CMake file. CMake was pointing me in the right direction with the warning: "This command specifies the relative path as a link directory. Policy CMP0015 is not set: link_directories() treats paths relative to the source dir. Run "cmake --help-policy CMP0015" for policy details. Use the cmake_policy command to set the policy and suppress this warning." Guess I should have listened!