首页 > 解决方案 > CMake 不会将依赖库添加到项目文件中

问题描述

我正在使用 pcl 编写一个简单的示例代码,就像链接 http://pointclouds.org/documentation/tutorials/writing_pcd.php一样。

即使我点击链接,Visual Studio 也会报告链接错误。原因是 pcl 依赖于 boost 库,而 cmake 没有将 boost 库添加到 Visual Studio 的项目设置文件中。如果我使用以下行添加 boost 库,一切都会好起来的。

target_link_libraries(程序名 ${Boost_LIBRARIES})

为什么 CMake 不处理这个?有更好的解决方案吗?

CMake:3.13.2,

视觉工作室:社区 2017

PCL:PCL-1.9.1-AllInOne-msvc2017-win64.exe

编辑: CMakeLists.txt

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(MY_GRAND_PROJECT)
set(Boost_DEBUG ON)

set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
find_package(Boost 1.45.0) 

find_package(PCL 1.9.1 REQUIRED COMPONENTS common io)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(pcd_write_test pcd_write.cpp)
target_link_libraries(pcd_write_test ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES}  ${Boost_LIBRARIES})

标签: visual-studioboostcmake

解决方案


根据官方 repo 中的PCLConfig.cmake脚本,Boost 库被附加到PCL_LIBRARIES变量中。(这是在脚本末尾附近执行的)。

因此,为了在使用 PCL 时自动链接 Boost 库,您需要使用PCL_LIBRARIES变量进行链接,而不是PCL_<comp>_LIBRARIES变量。


我不知道,给定的行为是否是有意的。如果您假设 Boost 库也应该是PCL_<comp>_LIBRARIES变量的一部分,那么您可以填写一份关于此的错误报告。


推荐阅读