Search code examples
c++visual-studio-codecmakevcpkg

vcpkg and cmake and vsc on Ubuntu can not find package


I installed vcpkg on Ubuntu 20.04 and install boost and opencv.

I have this cmakelist file:

set(CMAKE_TOOLCHAIN_FILE /home/m/local/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "")
set(VCPKG_TARGET_TRIPLET "x64-linux" CACHE STRING "")
cmake_minimum_required(VERSION 3.0.0)
project(test1 VERSION 0.1.0)
    
find_package(Opencv CONFIG REQUIRED)
find_package(boost CONFIG REQUIRED )
    
add_executable(test1 main.cpp)
target_link_libraries(test1 PRIVATE opencv::opencv boost::boost)

but when I run it inside Visual studio code, it can not find openCV and boost.

What is the problem and how can I solve it?


Solution

  • Case matters in CMake as mentioned in the comments:

    find_package(OpenCV CONFIG REQUIRED)
    

    and

    find_package(Boost REQUIRED)
    

    vcpkg does not install config files for Boost, so you cannot use CONFIG here. You probably also want to call it with the COMPONENTS option to list the modules you actually want.

    Usage from vcpkg:

    The package boost is compatible with built-in CMake targets:
    
        find_package(Boost REQUIRED [COMPONENTS <libs>...])
        target_link_libraries(main PRIVATE ${Boost_LIBRARIES})
        target_include_directories(main PRIVATE ${Boost_INCLUDE_DIRS})