如何通过 CMake 在 Visual Studio 2022 中链接已编译的 Boost 二进制文件?

问题描述 投票:0回答:1

我已经在我的

D:\
(不是默认路径)中下载了Boost,并使用

生成了所有二进制文件

 .\b2 -j4 link=static threading=multi runtime-link=shared --build-type=minimal stage --stagedir=stage/

然后我使用 CMake 创建了一个简单的 Visual Studio 2022 项目。 CMakeLists.txt的内容是:

cmake_minimum_required (VERSION 3.12)

# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)
  cmake_policy(SET CMP0141 NEW)
  set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()


set(BOOST_ROOT D:/boost_1_82_0)
set(Boost_INCLUDE_DIR ${BOOST_ROOT})
set(Boost_DEBUG ON)
set(Boost_NO_SYSTEM_PATHS TRUE)
set(Boost_NO_BOOST_CMAKE TRUE)
# set(Boost_USE_STATIC_LIBS_ON)
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIR})

project ("HelloCMake")

# Add source to this project's executable.
add_executable (HelloCMake "HelloCMake.cpp" "HelloCMake.h")

message(boost_libraries "${Boost_LIBRARIES}")
target_link_libraries(HelloCMake ${Boost_SYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_CONTAINER_LIBRARY})

只要它只需要像

boost::asio
这样的标头,它就可以正常运行,但是一旦我添加了任何需要像
boost/json/src.hpp
这样的编译二进制文件的头文件,Visual Studio就无法链接并给出错误
Error LNK1104 cannot open file 'libboost_container-vc143-mt-gd-x64-1_82.lib'

我是 cmake 新手,所以我可能会遗漏一些明显的东西。您看到 cmake 配置有任何错误吗?谢谢!

c++ visual-studio cmake boost visual-studio-2022
1个回答
0
投票

解决方案是禁用自动链接。我需要的额外配置是:

   # disable autolinking in boost
   add_definitions( -DBOOST_ALL_NO_LIB )

   # we already disabled autolinking, but we need this for intellisense errors to go away.
   # without this option VS will keep on complaining about boost libs not found but it will run successfully.
   add_definitions( -DBOOST_ALL_DYN_LINK )
© www.soinside.com 2019 - 2024. All rights reserved.