简体   繁体   中英

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 DLL s are "merged" into the final EXE .

How can I accomplish this?

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)

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 in this article .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM