CMake generated program fails to link on Windows: tries to link to non-existent file

2

I am trying to compile a very simple test program on Windows and keep getting linker errors. The program to link is the following:

#include <boost/asio/io_context.hpp>

int main()
{
    boost::asio::io_context context;
}

While the CMakeLists.txt looks like this:

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)

When building this using nmake, it fails with the following output:

-- 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~2\MICROS~1\2017\BUILDT~1\VC\Tools\MSVC\1414~1.264\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\windows-test.dir\objects1.rsp /out:windows-test.exe /implib:windows-test.lib /pdb:Z:\windows-test\build\windows-test.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console C:\local\boost_1_67_0\lib64-msvc-14.1\boost_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:CMakeFiles\windows-test.dir/intermediate.manifest CMakeFiles\windows-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 Files\CMake\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

The file 'libboost_system-vc141-mt-gd-x64-1_67.lib' indeed does not exist on the system, but I don't know where it is coming from, since it does not appear on the linker command that it is executing. The linker command shows the file C:\local\boost_1_67_0\lib64-msvc-14.1\boost_system-vc141-mt-gd-x64-1_67.lib, which does exist.

Why oh why does it try to link to a missing file, that appears nowhere on the linker command? I feel way out of my depth here, since I haven't had to use Windows for almost 20 years and never before had to port to it.

c++
windows
boost
cmake
asked on Stack Overflow Jul 3, 2018 by Martijn Otto • edited Jul 3, 2018 by Martijn Otto

1 Answer

1

Boost headers contain linker commands on Windows, so that Boost libraries are linked automatically when including the appropriate header. However, it seems your setup uses a different naming scheme for the libraries, which makes these fail to link.

You can disable the Boost auto-linking feature by defining the preprocessor macro BOOST_ALL_NO_LIB. Like this:

target_compile_definitions(windows-test PRIVATE BOOST_ALL_NO_LIB)

User contributions licensed under CC BY-SA 3.0