6

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,

4
  • Use c++filt to see what the real function names are -- the seemingly random characters are a result of name mangling in C++ Commented Jul 6, 2014 at 23:31
  • 2
    ok. I did nm libdsp.so | c++filt | grep -i gen_lpf to find the library I was looking for. That was pretty awesome. Thanks for the tip.
    – jrk0414
    Commented Jul 6, 2014 at 23:36
  • It seems to be aproblem with your referenced library, which does not contain these things. Have a look there and try to check the lib contents from this one with objdump. MAybe you just forgot to compile them into the lib or the compiler optimizes them away.
    – Sven
    Commented Jul 7, 2014 at 11:21
  • 1
    When I do nm, I see everything it tells me that it can't find. Same thing with objdump.
    – jrk0414
    Commented Jul 7, 2014 at 14:43

1 Answer 1

4

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)
2
  • 1
    Amazingly, that fixed it. I would never have thought that would be the problem. Thanks for the help.
    – jrk0414
    Commented Jul 8, 2014 at 2:01
  • 1
    I was banging my head over an apbs: symbol lookup error: [...] undefined symbol: X error. Turns out it was caused by an include_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!
    – ostrokach
    Commented Jan 28, 2017 at 4:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.