0

I am trying to compile and run C++ tests with CTest and Boost's unit test framework, linked dynamically.

CMakeLists.txt contains this (unrelated commands excluded):

[...]

find_package(Boost REQUIRED COMPONENTS Unit_test_framework)
enable_testing()

file(GLOB MY_PROGRAM_TEST_SRCS src/test/*.cpp)
add_executable(MyProgramTests ${MY_PROGRAM_TEST_SRCS})
target_compile_definitions(MyProgramTests PRIVATE "BOOST_TEST_DYN_LINK")
target_link_libraries(
    MyProgramTests PRIVATE
    Boost::unit_test_framework
)

add_test(NAME MyProgramTests COMMAND MyProgramTests)

src/test/tests.cpp contains this:

/* This file tests that tests do indeed test */

#define BOOST_TEST_MODULE LxCygprodLauncherTests
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(test_tests_do_test)
{
    BOOST_TEST(true);
}

Other information:

  • I have checked that the Boost::unit_test_framework target links to D:\libraries-debug\lib\boost_unit_test_framework-vc142-mt-gd-x64-1_79.lib, which exists.
  • Boost was compiled with Visual Studio 2019 (vc142) in debug mode.
  • Linking to other Boost targets, like Boost::filesystem, works.

When I build this with Visual Studio 2022 (vc143) in debug mode, I get this linker error:

  [26/394] Linking CXX executable app\MyProgram\MyProgramTests.exe
  FAILED: app/MyProgram/MyProgramTests.exe 
  cmd.exe /C "cd . && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=app\MyProgram\CMakeFiles\MyProgramTests.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x64\mt.exe --manifests  -- C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1435~1.322\bin\Hostx64\x64\link.exe /nologo app\MyProgram\CMakeFiles\MyProgramTests.dir\MyProgramTests_autogen\mocs_compilation.cpp.obj app\MyProgram\CMakeFiles\MyProgramTests.dir\src\test\test_tests.cpp.obj  /out:app\MyProgram\MyProgramTests.exe /implib:app\MyProgram\MyProgramTests.lib /pdb:app\MyProgram\MyProgramTests.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console -LIBPATH:D:\libraries-debug\cuda\lib\x64 D:\libraries-debug\lib\boost_unit_test_framework-vc142-mt-gd-x64-1_79.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
  LINK Pass 1: command "C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1435~1.322\bin\Hostx64\x64\link.exe /nologo app\MyProgram\CMakeFiles\MyProgramTests.dir\MyProgramTests_autogen\mocs_compilation.cpp.obj app\MyProgram\CMakeFiles\MyProgramTests.dir\src\test\test_tests.cpp.obj /out:app\MyProgram\MyProgramTests.exe /implib:app\MyProgram\MyProgramTests.lib /pdb:app\MyProgram\MyProgramTests.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console -LIBPATH:D:\libraries-debug\cuda\lib\x64 D:\libraries-debug\lib\boost_unit_test_framework-vc142-mt-gd-x64-1_79.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:app\MyProgram\CMakeFiles\MyProgramTests.dir/intermediate.manifest app\MyProgram\CMakeFiles\MyProgramTests.dir/manifest.res" failed (exit code 1104) with the following output:
C:\Users\Me\code\cpp-tests\src\out\build\x64-Debug\LINK : fatal error LNK1104: cannot open file 'boost_unit_test_framework-vc143-mt-gd-x64-1_79.lib'

Why does LINK.EXE try to open boost_unit_test_framework-vc143-mt-gd-x64-1_79.lib (vc143), when the command asks it to link to boost_unit_test_framework-vc142-mt-gd-x64-1_79.lib (vc142)?

Edit 1

When running the link command with /verbose in a Visual Studio 2022 developer shell, the log starts with:

Starting pass 1
Processed /DEFAULTLIB:MSVCRTD
Processed /DEFAULTLIB:OLDNAMES
Processed /DEFAULTLIB:msvcprtd
Processed /DEFAULTLIB:boost_unit_test_framework-vc143-mt-gd-x64-1_79.lib

Searching libraries

The link command does not contain /defaultlib.

Arno
  • 103
  • 2
  • 5
  • 1
    [That answer](https://stackoverflow.com/a/5249569/3440745) suggests that `/DEFAULTLIB` may be emiited because of `#pragma comment(lib...)`. Probably, that pragma is a part of auto-generated `mocs_compilation.cpp`. – Tsyvarev Mar 13 '23 at 21:49

0 Answers0