Monhde Sau Hung
Monhde Sau Hung

Reputation: 41

How can vcpkg-installed-lib and single-down-load lib use together in CLion

Case 1. I usually install cxx libs with vcpkg. I use Clion to build and run my project. My CLion version is 2024.1.1

I set -DCMAKE_TOOLCHAIN_FILE=/path/to/my/vcpkg/scripts/buildsystems/vcpkg.cmake on CLion-settings. The setting path is: Settings - Build, Execution, Deployment - CMake - CMake options, as the screenshot shows: enter image description here

Case 2 Sometimes I also download the source code and compile it without vcpkg. I use the boost_1_85_0 recently. I compiled and install the version boost in a specific path. If I set the CMAKE_TOOLCHAIN_FILE as Case 1 does, CLion can not find the boost package. So I delete the CMAKE_TOOLCHAIN_FILE setting on CLion. And add the CMAKE_PREFIX_PATH setting in CMakeLists.txt as followed:

cmake_minimum_required(VERSION 3.24)
set(PROJECT_NAME vcpkg_and_singlelib)
project(${PROJECT_NAME})

set(CMAKE_CXX_STANDARD 17)

add_executable(${PROJECT_NAME} main.cpp)

set(BOOST_ROOT "Path/to/boost/installed/boost_1_85_0_install_default")
set(CMAKE_PREFIX_PATH        "${BOOST_ROOT}")
find_package(Boost 1.85.0 REQUIRED COMPONENTS system thread date_time log log_setup program_options)
target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE Boost::system Boost::thread Boost::date_time Boost::log Boost::log_setup Boost::program_options)

find_package(RapidJSON CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE rapidjson)

But the CLion can not find the packages installed by vcpkg like rapidjson as above showed.

Question: How can I find packages from both vcpkg and self-installed directory?

I set CMAKE_TOOLCHAIN_FILE before vcpkg-installed packages and after self-installed packages.

cmake_minimum_required(VERSION 3.24)
set(PROJECT_NAME vcpkg_and_singlelib)
project(${PROJECT_NAME})

set(CMAKE_CXX_STANDARD 17)

add_executable(${PROJECT_NAME} main.cpp)

set(BOOST_ROOT "Path/to/boost/installed/boost_1_85_0")
set(CMAKE_PREFIX_PATH        "${BOOST_ROOT}")
find_package(Boost 1.85.0 REQUIRED COMPONENTS system thread date_time log log_setup program_options)
target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE Boost::system Boost::thread Boost::date_time Boost::log Boost::log_setup Boost::program_options)

set(CMAKE_TOOLCHAIN_FILE "/path/to/my/vcpkg/scripts/buildsystems/vcpkg.cmake")

find_package(RapidJSON CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE rapidjson)

But the vcpkg-installed packages still can not be found.

to reply: I set the CMAKE_TOOLCHAIN_FILE on clion, and append BOOST_ROOT to CMAKE_PREFIX_PATH.

Now the CLion can not find the boost of right version.

The CMake on Clion shows:

CMake Error at C:/Path_to_User_Home/program/cmake-3.24.2-windows-x86_64/share/cmake-3.24/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Boost: Found unsuitable version "1.84.0", but required is at
  least "1.85.0" (found
  C:/Path_to_User_Home/program/vcpkg-2024.04.26/installed/x64-windows/include,
  found components: system thread date_time log log_setup program_options
  chrono atomic filesystem regex)
Call Stack (most recent call first):
  C:/Path_to_User_Home/program/cmake-3.24.2-windows-x86_64/share/cmake-3.24/Modules/FindPackageHandleStandardArgs.cmake:592 (_FPHSA_FAILURE_MESSAGE)
  C:/Path_to_User_Home/program/cmake-3.24.2-windows-x86_64/share/cmake-3.24/Modules/FindBoost.cmake:2376 (find_package_handle_standard_args)
  C:/Path_to_User_Home/program/vcpkg-2024.04.26/installed/x64-windows/share/boost/vcpkg-cmake-wrapper.cmake:11 (_find_package)
  C:/Path_to_User_Home/program/vcpkg-2024.04.26/scripts/buildsystems/vcpkg.cmake:813 (include)
  CMakeLists.txt:15 (find_package)


-- Configuring incomplete, errors occurred!
See also "C:/Path_to_User_Home/CLionProjects/vcpkg_and_singlelib/cmake-build-release/CMakeFiles/CMakeOutput.log".
See also "C:/Path_to_User_Home/CLionProjects/vcpkg_and_singlelib/cmake-build-release/CMakeFiles/CMakeError.log".

[Failed to reload]

Upvotes: 0

Views: 71

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66088

Variable CMAKE_PREFIX_PATH is treated by CMake as a list, so it can accommodate several paths. Your code overwrites the variable, so any content added by vcpkg is lost. Instead, append new paths to it:

list(APPEND CMAKE_PREFIX_PATH "${BOOST_ROOT}")

Upvotes: 1

Related Questions