Search code examples
c++boostcmakevcpkg

Unable to find Boost libraries with CMake and vcpkg


I have installed boost-variant2 library using the vcpkg command:

vcpkg install boost-variant2:x64-windows

When vcpkg finished the installation, it prompted this:

The package boost is compatible with built-in CMake targets:

    find_package(Boost REQUIRED [COMPONENTS <libs>...])
    target_link_libraries(main PRIVATE Boost::boost Boost::<lib1> Boost::<lib2> ...)

so in my CMakeLists.txt I added the following lines:

find_package(Boost COMPONENTS variant2 REQUIRED)
target_link_libraries(MyTarget PRIVATE Boost::variant2)

However, when I run cmake -DCMAKE_TOOLCHAIN_FILE:STRING=/path_to_vcpkg/scripts/buildsystems/vcpkg.cmake I get the following error:

-- Configuring incomplete, errors occurred!
Could NOT find Boost (missing: variant2) (found version "1.78.0")


Solution

  • Looks like variant2 is header-only lib and you can just use Cmake file like this:

    cmake_minimum_required(VERSION 3.5)
    
    project(project LANGUAGES CXX)
    
    set(CMAKE_CXX_STANDARD 20)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Boost)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(project main.cpp)
    

    U can see list of libs required to be built for here for Windows and here for Unix-like systems