CMake 生成的程序无法在 Windows 上链接:尝试链接到不存在的文件
CMake generated program fails to link on Windows: tries to link to non-existent file
我正在尝试在Windows上编译一个非常简单的测试程序,但不断收到链接器错误。要链接的程序如下:
#include <boost/asio/io_context.hpp>
int main()
{
boost::asio::io_context context;
}
虽然CMakeLists.txt看起来像这样:
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(windows-test)
SET(CMAKE_CXX_STANDARD 17)
find_package(Boost 1.6.7 COMPONENTS system)
include_directories("${Boost_INCLUDE_DIRS}")
add_executable(windows-test main.cpp)
target_link_libraries(windows-test Boost::system)
使用 nmake 构建此内容时,它会失败并显示以下输出:
-- Boost version: 1.67.0
-- Found the following Boost libraries:
-- system
-- Configuring done
-- Generating done
-- Build files have been written to: Z:/windows-test/build
[ 50%] Linking CXX executable windows-test.exe
LINK Pass 1: command "C:PROGRA~2MICROS~12017BUILDT~1VCToolsMSVC1414~1.264binHostx64x64link.exe /nologo @CMakeFileswindows-test.dirobjects1.rsp /out:windows-test.exe /implib:windows-test.lib /pdb:Z:windows-testbuildwindows-test.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console C:localboost_1_67_0lib64-msvc-14.1boost_system-vc141-mt-gd-x64-1_67.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFileswindows-test.dir/intermediate.manifest CMakeFileswindows-test.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: cannot open file 'libboost_system-vc141-mt-gd-x64-1_67.lib'
NMAKE : fatal error U1077: '"C:Program FilesCMakebincmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2017BuildToolsVCToolsMSVC14.14.26428binHostX64x64nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2017BuildToolsVCToolsMSVC14.14.26428binHostX64x64nmake.exe"' : return code '0x2'
Stop.
文件'libboost_system-vc141-mt-gd-x64-1_67.lib'确实存在于系统上,但我不知道它来自哪里,因为它没有出现在它正在执行的链接器命令上。链接器命令显示文件 C:\local\boost_1_67_0\lib64-msvc-14.1\boost_system-vc141-mt-gd-x64-1_67.lib,该文件确实存在。
为什么哦,为什么它会尝试链接到链接器命令中没有出现的丢失文件?我觉得这里超出了我的深度,因为我已经将近 20 年没有使用 Windows,以前也从来没有移植到它。
Boost 标头包含Windows 上的链接器命令,因此在包含适当的标头时会自动链接 Boost 库。但是,您的设置似乎对库使用了不同的命名方案,这使得这些库无法链接。
您可以通过定义预处理器宏BOOST_ALL_NO_LIB
来禁用 Boost 自动链接功能。喜欢这个:
target_compile_definitions(windows-test PRIVATE BOOST_ALL_NO_LIB)
相关文章:
- 无法链接文件系统库C++
- 如何在C++中链接 .a 文件
- 在为 Java 编译动态链接文件时,我应该选择哪些 g++ 参数
- Makefile 将在文件更改时链接文件,但不会重新编译更改的源文件
- CMake 从子目录和父目录链接文件
- 如何将GCC链接器固定地址添加到链接文件中
- 在GCC中链接.h文件
- 使用GDB中的多个链接文件进行调试
- visualstudio2010表达了不同项目中的链接文件
- 编译器是否会优化未使用的链接文件
- 尝试取消链接文件时编译 C++ 时出错
- 链接文件错误,VS 2015中的错误LNK2005
- 用C++中的链接文件编译错误
- Makefile未链接.o文件
- 如何链接.h文件中声明为extern的函数,以创建.dll文件
- 通过makefiles链接文件
- 未定义的引用错误,即使在链接c文件之后
- 多个链接c++文件
- 使用CMakeLists.txt链接c++文件
- 在c++中链接文件