使用cmake的共享库的未定义引用 [英] undefined reference with shared library using cmake

查看:2233
本文介绍了使用cmake的共享库的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了很多地方找到这个答案,但我还没有找到任何关于我的情况。这似乎很容易,这就是为什么这是如此令人沮丧。

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.

我正在用CMAKE建立一个项目。我生成两个共享库。一个包括另一个。我也生成一个可执行文件。可执行文件链接到封装另一个的共享库。以下是代码的相关部分:

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)

得到。

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)'

还有更多的人来自那里。我已经尝试使用静态库,但后来它只是延迟问题,直到下一个可执行文件,我试图包括这些库。我也试过使用g ++而不是c ++。我试过交换图书馆订单。此外,它不是一个模板问题,因为它不能找到的至少一个引用不是模板函数。我已经搜索了图书馆的符号,我可以找到他们,虽然他们是前置和后置的随机字符。

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.

感谢,

UPDATE

我找到了另一个链接,特别是这一个,提到了add_subdirectory的潜在问题。这是另一个项目的子项目。所以我送进了这个项目并建在那里。有效!它仍然不工作在父目录尽管。也许这可以给某人的线索。

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.

再次感谢,

推荐答案

包含CMakeLists.txt是运行时的当前目录。相反,每当需要相对于CMakeLists.txt的路径时,使用 $ {CMAKE_CURRENT_SOURCE_DIR} 。例如:

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)

这篇关于使用cmake的共享库的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆