undefined reference with shared library using cmake

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

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,

回答1:

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) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!