12

I'm using the package manager vcpkg to install the (static) Boost libraries via vcpkg install boost:x64-windows-static.

Furthermore, I use CMake as my build system and I'm passing C:\vcpkg\scripts\buildsystems\vcpkg.cmake to CMake via the -DCMAKE_TOOLCHAIN_FILE CMake command.

In my CMakeLists.txt I force static Boost libraries:

set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS filesystem iostreams REQUIRED)
if (Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})
endif ()

# ...

target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})

However, Visual Studio still tries to look at the wrong file path for my Boost libraries:

Error 'C:/vcpkg/installed/x64-windows/lib/boost_filesystem-vc140-mt.lib', needed by 'MyProject.exe', missing and no known rule to make it

If I install the dynamic Boost libraries, it will build fine since this is where Visual Studio looks. However, I want to use the static libraries in my build instead so that all DLLs are "merged" into the final EXE.

How can I accomplish this?

Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • If the path `C:/vcpkg/installed/x64-windows/lib/boost_filesystem-vc140-mt.lib` is wrong, what is the **correct path** to the static library on your machine? According to the Boost library naming convensions, `boost_filesystem-vc140-mt.lib` is actually a static library file. – Tsyvarev Feb 09 '20 at 05:53
  • 1
    @Tsyvarev: The correct path would be `C:/vcpkg/installed/x64-windows-static/lib/boost_filesystem-vc140-mt.lib`. This is how `vcpkg` sets it up when I install the static libraries. – BullyWiiPlaza Feb 09 '20 at 10:11

3 Answers3

4

I had the same problem.

Solved with

define  set(Boost_INCLUDE_DIR "path")

before find_package force.

When you using cmake with vcpkg, find _VCPKG_INSTALLED_DIR variable in CmakeCache.txt

set(Boost_INCLUDE_DIR ${_VCPKG_INSTALLED_DIR}/x64-windows-static/include)
Cristiano Casciotti
  • 1,016
  • 10
  • 21
커피맨
  • 49
  • 2
1

It seems that by default vcpkg.cmake script auto-detect dynamic libraries that are installed by vcpkg.

You can override this behavior for your project by setting a variable when invoking cmake:

cmake .. -DCMAKE_TOOLCHAIN_FILE=.../vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static

Or using the CMakePresets.json:

"cacheVariables": {
 ...
  "CMAKE_TOOLCHAIN_FILE": {
    "value": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
    "type": "FILEPATH"
  },
  "VCPKG_TARGET_TRIPLET": "x64-windows-static"
},

I haven't yet found how to do this on a per library basis.

More details here and here.

Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37
0

Actually, what I have found to work correctly is the following CMake:

# Boost settings
set(Boost_USE_STATIC_LIBS        ON)  # only find static libs
set(Boost_USE_DEBUG_LIBS        OFF)  # ignore debug libs and
set(Boost_USE_RELEASE_LIBS       ON)  # only find release libs
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME     ON)  # link Boost Static libraries
ferchor2003
  • 103
  • 9