# Problem to link with Boost

**URL:** <https://discourse.cmake.org/t/problem-to-link-with-boost/12787>\
**Category:** Code\
**Created:** [October 16, 2024, 9:45am UTC](https://discourse.cmake.org/t/problem-to-link-with-boost/12787 "2024-10-16T09:45:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [October 16, 2024, 9:45am UTC](https://discourse.cmake.org/t/problem-to-link-with-boost/12787/1 "2024-10-16T09:45:38Z")

</div>

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:

```auto
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:

```auto
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 :

```auto
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:

```auto
  -- 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()` ?

---

<div class="post-metadata">

**Author:** ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)\
**Post date:** [October 16, 2024, 9:57am UTC](https://discourse.cmake.org/t/problem-to-link-with-boost/12787/2 "2024-10-16T09:57:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [October 16, 2024, 9:57am UTC](https://discourse.cmake.org/t/problem-to-link-with-boost/12787/3 "2024-10-16T09:57:41Z")

</div>

The `lib` prefix is for static ones.

---

<div class="post-metadata">

**Author:** ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)\
**Post date:** [October 16, 2024, 10:02am UTC](https://discourse.cmake.org/t/problem-to-link-with-boost/12787/4 "2024-10-16T10:02:55Z")

</div>

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?)

---

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [October 16, 2024, 10:05am UTC](https://discourse.cmake.org/t/problem-to-link-with-boost/12787/5 "2024-10-16T10:05:31Z")

</div>

The prefix is added by Boost:

[https://www.boost.org/doc/libs/1\_85\_0/libs/test/doc/html/boost\_test/section\_faq.html](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?** )

---

<div class="post-metadata">

**Author:** ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)\
**Post date:** [October 16, 2024, 7:58pm UTC](https://discourse.cmake.org/t/problem-to-link-with-boost/12787/6 "2024-10-16T19:58:21Z")

</div>

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