windows 无法更改编译器版本FindBoost.cmake在Boost中搜索,导致VS2019和VS2022出现链接器错误

aelbi1ox  于 2023-05-08  发布在  Windows
关注(0)|答案(1)|浏览(254)

在使用Boost时,我遇到了一个问题,因为修改FindBoost.cmake正在搜索的编译器版本。默认情况下,它会搜索vc 140工具集,但我尝试在Visual Studio 2019和Visual Studio 2022中使用vc 142或vc 143工具集。
这是我的CMakeList.txt,我试图在其中为findboost设置正确的提示。不管我做什么,_boost_COMPILER仍然是vc 140。

set(BOOST_ROOT "D:/Bladesense/boost_1_82_0")

set(Boost_DEBUG ON)
set(Boost_COMPILER "-vc143")
set(BOOST_COMPILER  "-vc143")
set(_boost_COMPILER "-vc143")
message(STATUS "BOOST_COMPILER ${BOOST_COMPILER}")
message(STATUS "MSVC_TOOLSET_VERSION ${MSVC_TOOLSET_VERSION}")

find_package(Boost 1.82 REQUIRED COMPONENTS serialization filesystem system)

这是FindBoost调试输出(最后一行的relevant _boost_COMPILER):

[CMake] -- MSVC_TOOLSET_VERSION 142
1> [CMake] -- [ :1658 ] _boost_TEST_VERSIONS = <unset>
1> [CMake] -- [ :1659 ] Boost_USE_MULTITHREADED = "ON"
1> [CMake] -- [ :1660 ] Boost_USE_STATIC_LIBS = "OFF"
1> [CMake] -- [ :1661 ] Boost_USE_STATIC_RUNTIME = <unset>
1> [CMake] -- [ :1662 ] Boost_ADDITIONAL_VERSIONS = <unset>
1> [CMake] -- [ :1663 ] Boost_NO_SYSTEM_PATHS = <unset>
1> [CMake] -- [ :1695 ] BOOST_ROOT = "D:/Bladesense/boost_1_82_0"
1> [CMake] -- [ :1696 ] ENV{BOOST_ROOT} = <unset>
1> [CMake] -- [ :1697 ] BOOST_INCLUDEDIR = <unset>
1> [CMake] -- [ :1698 ] ENV{BOOST_INCLUDEDIR} = <unset>
1> [CMake] -- [ :1699 ] BOOST_LIBRARYDIR = <unset>
1> [CMake] -- [ :1700 ] ENV{BOOST_LIBRARYDIR} = <unset>
1> [CMake] -- [ :1772 ] _boost_INCLUDE_SEARCH_DIRS = "D:/Bladesense/boost_1_82_0/include;D:/Bladesense/boost_1_82_0;PATHS;C:/boost/include;C:/boost;/sw/local/include"
1> [CMake] -- [ :1773 ] _boost_PATH_SUFFIXES = <unset>
1> [CMake] -- [ :1788 ] location of version.hpp: D:/Bladesense/boost_1_82_0/boost/version.hpp
1> [CMake] -- [ :1828 ] Boost_VERSION = "108200"
1> [CMake] -- [ :1829 ] Boost_VERSION_STRING = "1.82.0"
1> [CMake] -- [ :1830 ] Boost_VERSION_MACRO = "108200"
1> [CMake] -- [ :1831 ] Boost_VERSION_MAJOR = "1"
1> [CMake] -- [ :1832 ] Boost_VERSION_MINOR = "82"
1> [CMake] -- [ :1833 ] Boost_VERSION_PATCH = "0"
1> [CMake] -- [ :1834 ] Boost_VERSION_COUNT = "3"
1> [CMake] -- [ :1858 ] Boost_LIB_PREFIX = ""
1> [CMake] -- [ :1859 ] Boost_NAMESPACE = "boost"
1> [CMake] -- [ :1881 ] _boost_COMPILER = "-vc140" (user-specified via Boost_COMPILER)

我不明白为什么_boost_COMPILER仍然是-vc 140。当我安装使用vc 140工具集编译的boost时,CmakeCache.txt正确生成:

//Build shared libraries (DLLs).
BUILD_SHARED_LIBS:BOOL=OFF

//Boost filesystem library (debug)
Boost_FILESYSTEM_LIBRARY_DEBUG:FILEPATH=D:/Bladesense/boost_1_82_0/lib64-msvc-14.0/boost_filesystem-vc140-mt-gd-x64-1_82.lib

//Boost filesystem library (release)
Boost_FILESYSTEM_LIBRARY_RELEASE:FILEPATH=D:/Bladesense/boost_1_82_0/lib64-msvc-14.0/boost_filesystem-vc140-mt-x64-1_82.lib

//Path to a file.
Boost_INCLUDE_DIR:PATH=D:/Bladesense/boost_1_82_0

//Boost library directory DEBUG
Boost_LIBRARY_DIR_DEBUG:PATH=D:/Bladesense/boost_1_82_0/lib64-msvc-14.0

//Boost library directory RELEASE
Boost_LIBRARY_DIR_RELEASE:PATH=D:/Bladesense/boost_1_82_0/lib64-msvc-14.0

//Boost serialization library (debug)
Boost_SERIALIZATION_LIBRARY_DEBUG:FILEPATH=D:/Bladesense/boost_1_82_0/lib64-msvc-14.0/boost_serialization-vc140-mt-gd-x64-1_82.lib

//Boost serialization library (release)
Boost_SERIALIZATION_LIBRARY_RELEASE:FILEPATH=D:/Bladesense/boost_1_82_0/lib64-msvc-14.0/boost_serialization-vc140-mt-x64-1_82.lib

//Boost system library (debug)
Boost_SYSTEM_LIBRARY_DEBUG:FILEPATH=D:/Bladesense/boost_1_82_0/lib64-msvc-14.0/boost_system-vc140-mt-gd-x64-1_82.lib

//Boost system library (release)
Boost_SYSTEM_LIBRARY_RELEASE:FILEPATH=D:/Bladesense/boost_1_82_0/lib64-msvc-14.0/boost_system-vc140-mt-x64-1_82.lib

然而,在构建过程中我得到了一个链接器错误。

[37/181] cmd.exe /C "cd . && D:\VS\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\bin2c.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests  -- D:\VS\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\link.exe /nologo CMakeFiles\bin2c.dir\bin2c_autogen\mocs_compilation.cpp.obj CMakeFiles\bin2c.dir\libvis\src\bin2c\main.cc.obj  /out:bin2c.exe /implib:bin2c.lib /pdb:bin2c.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console  D:\Bladesense\boost_1_82_0\lib64-msvc-14.0\boost_serialization-vc140-mt-gd-x64-1_82.lib  D:\Bladesense\boost_1_82_0\lib64-msvc-14.0\boost_filesystem-vc140-mt-gd-x64-1_82.lib  D:\Bladesense\boost_1_82_0\lib64-msvc-14.0\boost_system-vc140-mt-gd-x64-1_82.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cmd.exe /C "cd /D D:\Bladesense\badslam\out\build\x64-Debug && C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Bypass -file D:/Bladesense/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary D:/Bladesense/badslam/out/build/x64-Debug/bin2c.exe -installedDir D:/Bladesense/vcpkg/installed/x64-windows/debug/bin -OutVariable out""
  FAILED: bin2c.exe 
  cmd.exe /C "cd . && D:\VS\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\bin2c.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests  -- D:\VS\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\link.exe /nologo CMakeFiles\bin2c.dir\bin2c_autogen\mocs_compilation.cpp.obj CMakeFiles\bin2c.dir\libvis\src\bin2c\main.cc.obj  /out:bin2c.exe /implib:bin2c.lib /pdb:bin2c.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console  D:\Bladesense\boost_1_82_0\lib64-msvc-14.0\boost_serialization-vc140-mt-gd-x64-1_82.lib  D:\Bladesense\boost_1_82_0\lib64-msvc-14.0\boost_filesystem-vc140-mt-gd-x64-1_82.lib  D:\Bladesense\boost_1_82_0\lib64-msvc-14.0\boost_system-vc140-mt-gd-x64-1_82.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cmd.exe /C "cd /D D:\Bladesense\badslam\out\build\x64-Debug && C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Bypass -file D:/Bladesense/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary D:/Bladesense/badslam/out/build/x64-Debug/bin2c.exe -installedDir D:/Bladesense/vcpkg/installed/x64-windows/debug/bin -OutVariable out""
  LINK Pass 1: command "D:\VS\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\link.exe /nologo CMakeFiles\bin2c.dir\bin2c_autogen\mocs_compilation.cpp.obj CMakeFiles\bin2c.dir\libvis\src\bin2c\main.cc.obj /out:bin2c.exe /implib:bin2c.lib /pdb:bin2c.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console D:\Bladesense\boost_1_82_0\lib64-msvc-14.0\boost_serialization-vc140-mt-gd-x64-1_82.lib D:\Bladesense\boost_1_82_0\lib64-msvc-14.0\boost_filesystem-vc140-mt-gd-x64-1_82.lib D:\Bladesense\boost_1_82_0\lib64-msvc-14.0\boost_system-vc140-mt-gd-x64-1_82.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\bin2c.dir/intermediate.manifest CMakeFiles\bin2c.dir/manifest.res" failed (exit code 1104) with the following output:
D:\Bladesense\badslam\LINK : fatal error LNK1104: cannot open file 'libboost_filesystem-vc142-mt-gd-x64-1_82.lib'

我不明白为什么它试图链接libboost_filesystem-vc 142-mt-gd-x64-1_82.lib。libboost_filesystem-vc 140-mt-gd-x64-1_82.lib存在于目录中,并在CMakeCache.txt中指向。巧合的是,vc 142对应于我当前的msvc工具集版本。当我使用boost版本1.69或更低版本时,我得到了与libboost_filesystem-vc 141-mt-gd-x64-1_69.lib相同的链接器错误。
该问题在Boost版本1.55、1.69和1.82中仍然存在,使用自编译和预编译库。
作为解决这个问题的方法,我尝试使用vcpkg,它可以工作。但是,我需要vcpkg中没有的boost 1.55版本。

bxjv4tth

bxjv4tth1#

这里遇到的行为显然仍然是vcpkg集成的结果,即使安装了非vcpkg版本的Boost。这是因为vcpkg对Boost有一个特殊的情况,在使用find_package和vcpkg集成时,它会覆盖任何用户定义的Boost_COMPILER设置。以下链接提供了更多信息:
vcpkg GitHub上的问题讨论
vcpkg.cmake中的相关代码
在我的特殊情况下,我仍然需要vcpkg用于其他库,所以我想出了一个不太理想的解决方案。因为我不能修改vcpkg强制的编译器版本,所以我创建了一个DLL的副本(用正确的vc工具链编译),并重命名它们以匹配vcpkg错误地查找的版本。令人惊讶的是,链接过程仍然使用正确的DLL版本,使我的程序能够成功构建。不建议使用此解决方法,应谨慎使用。

相关问题