c++ - target_link_libraries 调用后链接路径困惑

标签 c++ gcc cmake matlab-deployment

我有一个 cmake 项目,我想在其中添加一个包含 matlab 引擎的类。为了编译它,我需​​要包含两个库 engmx ,我通过添加

target_link_libraries( ${TARGET} /usr/local/MATLAB/R2013b/bin/glnxa64/libeng.so)
target_link_libraries( ${TARGET} /usr/local/MATLAB/R2013b/bin/glnxa64/libmx.so)

到我的CMakeLists.txt文件。
然而,/usr/local/MATLAB/R2013b/bin/glnxa64/ 中还有许多其他旧版本的库。 , 这似乎 在调用上述命令时也会自动添加到路径中。我认为这会导致编译器找不到我的 不再使用普通库并产生错误。
我怎样才能只包含上面的两个库,而不是 glnxa64 中的所有其他库?文件夹?


运行后显示的警告 cmake . :

CMake Warning at CMakeLists.txt:23 (add_executable):  
Cannot generate a safe runtime search path for target CCDWidget because
files in some directories may conflict with libraries in implicit
directories:  

runtime library [libboost_program_options.so.1.49.0] in /usr/lib may be hidden by files in:  
/usr/local/MATLAB/R2013b/bin/glnxa64  
runtime library [libboost_system.so.1.49.0] in /usr/lib may be hidden by files in:  
/usr/local/MATLAB/R2013b/bin/glnxa64  
runtime library [libboost_filesystem.so.1.49.0] in /usr/lib may be hidden by files in:  
/usr/local/MATLAB/R2013b/bin/glnxa64  
runtime library [libboost_regex.so.1.49.0] in /usr/lib may be hidden by files in:  
/usr/local/MATLAB/R2013b/bin/glnxa64  

Some of these libraries may not be found correctly.  

链接时的错误信息:

Linking CXX executable CCDWidget  
/usr/lib/x86_64-linux-gnu/libharfbuzz.so.0: undefined reference to `FT_Face_GetCharVariantIndex'  
/usr/lib/x86_64-linux-gnu/libharfbuzz.so.0: undefined reference to `FT_Get_Advance'  
collect2: error: ld returned 1 exit status  
make[2]: ***  [CCDWidget] Error 1   
make[1]: *** [CMakeFiles/CCDWidget.dir/all] Error 2  
make: *** [all] Error 2  

下面是我的完整 CMakeLists.txt文件。用两个 ## 注释掉的行是我以前尝试过但没有解决我的问题的替代方法。 我还添加了 LINK_PRIVATE到 target_link_libraries 命令,如下面的代码所示,这没有什么区别。 选项 PRIVATE单独似乎不被我的 cmake 版本接受,因为它将错误消息更改为

/usr/bin/ld: cannot find -lPRIVATE  
collect2: error: ld returned 1 exit status  

#eng行被注释掉,编译和链接工作没有错误 (调用matlab引擎时也在Readout.cpp中注释掉了),所以错误一定是那一行产生的。


#Specify the version being used as well as the language  
cmake_minimum_required(VERSION 2.6)  
##cmake_policy(SET CMP0003 NEW)  
#Name your project here  
project(CCDWidget)  
set(TARGET CCDWidget)  

set(MAIN_SOURCES CCDWidget.cpp main.cc CCDControl.cpp VideoWindow.cpp ImageWindow.cpp ThisMeasurement.cpp KineticSeries.cpp FastKinetics.cpp Readout.cpp)  

##SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)  

#set_source_files_properties(Readout.cpp PROPERTIES COMPILE_FLAGS "-I/usr/local/MATLAB/R2013b/extern/include -I/usr/local/MATLAB/R2013b/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -I/usr/local/MATLAB/R2013b/extern/include/cpp -I/usr/local/MATLAB/R2013b/extern/include -DGLNXA64 -DGCC  -DMX_COMPAT_32 -DNDEBUG -Wno-effc++")  

find_package(Boost COMPONENTS program_options system filesystem regex REQUIRED)  
find_package(PkgConfig REQUIRED)  
pkg_check_modules(GTKMM gtkmm-3.0)  

include_directories( ${GTKMM_INCLUDE_DIRS} )  
include_directories( ${Boost_INCLUDE_DIR} )  
include_directories( ${PROJECT_SOURCE_DIR} )  
##link_directories(/usr/local/MATLAB/R2013b/bin/glnxa64)  
##target_link_libraries( ${TARGET} eng)  
##target_link_libraries( ${TARGET} mx)  

set(CMAKE_CXX_FLAGS "--std=c++11")  
add_executable( ${TARGET} ${MAIN_SOURCES} )  

target_link_libraries( ${TARGET} ${GTKMM_LIBRARIES} )  
target_link_libraries( ${TARGET} ${Boost_LIBRARIES} )  
target_link_libraries( ${TARGET} LINK_PRIVATE /usr/local/MATLAB/R2013b/bin/glnxa64/libeng.so)  # eng  
#target_link_libraries( ${TARGET} LINK_PRIVATE /usr/local/MATLAB/R2013b/bin/glnxa64/libmx.so ) # mx  
target_link_libraries( ${TARGET} andor )  

最佳答案

您可以尝试使用 imported target :

add_library(eng SHARED IMPORTED) 
set_property(TARGET eng PROPERTY IMPORTED_LOCATION /usr/local/MATLAB/R2013b/bin/glnxa64/libeng.so) 
... 
add_executable( ${TARGET} ${MAIN_SOURCES} ) 
... 
target_link_libraries(${TARGET} eng) 

对于调试,您可以尝试使用“ma​​ke VERBOSE=1”进行构建。
这将向您展示使用过的 gcc 命令行。 CMake 可能会将您的 target_link_libraries 命令转换为如下内容:

g++ ... -L/usr/local/MATLAB/R2013b/bin/glnxa64 -leng ... 

然后 gcc 会在这个文件夹中找到一些 boost 库。

关于c++ - target_link_libraries 调用后链接路径困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24161277/

相关文章:

c++ - 在同一台计算机上运行多个版本的 OpenCV

c++ - 在 C++ 中防止从 float 到 double 的隐式转换

c++ - 在 ARM 上初始化 uint32x4_t 时出现错误 C2078?

math - 为什么在 Math.h 中调用函数时会出现链接错误?

pointers - 64 位 gcc 代码中的 32 位指针溢出 - 编译失败

c++ - 从图像创建 MPEG4 视频时的分辨率问题 (Windows Media Foundation)

c++ - 如何仅在 Debug模式下向 cmake 添加符号?

c++ - CMake 添加库

c++ - 带有 CMake 和静态库/Z7 标志的 Visual Studio

c++ - 带有 gtest(C++) 的 CMakeLists C 程序