BullyWiiPlaza
BullyWiiPlaza

Reputation: 19225

Using static Boost libraries with vcpkg and CMake

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?

Upvotes: 14

Views: 14941

Answers (3)

ferchor2003
ferchor2003

Reputation: 123

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

Upvotes: 2

Elad Maimoni
Elad Maimoni

Reputation: 4595

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.

Upvotes: 4

커피맨
커피맨

Reputation: 69

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)

Upvotes: 6

Related Questions