Problem to link with Boost

Hello,

I am on CMake 3.29.3, and I want to link to Boost, on a Visual Studio environment.

Previously, I used Boost for only one version of MS C++ compiler, so the Boost directory structure was:

C:\boost\include
C:\boost\lib

I had then no problem to link.

Now, I want to use two versions of MS C++ compiler, and the Boost dirctories structure is:

C:\boost\boost   (inclusion files)
C:\boost\lib64-msvc-14.1
C:\boost\lib64-msvc-14.2
C:\boost\lib64-msvc-14.3

and I cannot link anymore.

Here is the CMakeLists.txt, run with CMake 3.29.3 :

find_package(Boost 1.85.0 EXACT
    REQUIRED COMPONENTS
        unit_test_framework)

if(Boost_FOUND)
    message(STATUS "@@@@ Boost found!")
    message(STATUS "@@@@ Boost_USE_STATIC_LIBS? ${Boost_USE_STATIC_LIBS}")
    message(STATUS "@@@@ ${Boost_LIBRARY_DIRS}")
    message(STATUS "@@@@ ${Boost_LIBRARIES}")
    message(STATUS "@@@@ ${Boost_unit_test_framework_FOUND}")
    message(STATUS "@@@@ ${Boost_unit_test_framework_LIBRARY}")
endif()

target_include_directories(MyApp
    PRIVATE
        ${Boost_INCLUDE_DIR})

target_link_libraries(MyApp
    PRIVATE
        Boost::unit_test_framework)

and here is the output:

  -- Found Boost: C:/Boost (found suitable exact version "1.85.0") found components: unit_test_framework
  -- @@@@ Boost found!
  -- @@@@ Boost_USE_STATIC_LIBS? ON
  -- @@@@ C:/Boost/lib64-msvc-14.1
  -- @@@@ optimized;C:/Boost/lib64-msvc-14.1/boost_unit_test_framework-vc141-mt-x64-1_85.lib;debug;C:/Boost/lib64-msvc-14.1/boost_unit_test_framework-vc141-mt-gd-x64-1_85.lib
  -- @@@@ ON
  -- @@@@
....
LINK : fatal error LNK1104: cannot open file 'libboost_unit_test_framework-vc141-mt-x64-1_85.lib'

But the file libboost_unit_test_framework-vc141-mt-x64-1_85.lib is in C:/Boost/lib64-msvc-14.1/.

In the VS solution (.sln), the directory to the Boost binaries has not been added.
Should I use target_link_directories() ?

Hmm, one is named libboost…lib, the other boost…lib. For MSVC, libraries usually do not have a lib prefix.

The lib prefix is for static ones.

Still, the textual comparison fails: you try to link the one without lib prefix but something makes it want to link the one with lib prefix (pragma linking?)

The prefix is added by Boost:

https://www.boost.org/doc/libs/1_85_0/libs/test/doc/html/boost_test/section_faq.html

(search for Why did I get a linker error when compiling my test program?)

So either you specify the link directory, or you disable the linking by msvc-specific pragma statement in header file.