1

I am trying to use VCPKG and CMAKE on a cpp project and am using the CPR library. I have been struggling to figure out what could be the cause of this error, re ran the get-started guide and other tutorials / blogs that are using cpr with vcpkg and is running fine with almost the exact same cmake config. What am I doing wrong?

I have ran the follow commands

vcpkg install 
vcpkg integrate install 

The cmake config I am using

cmake_minimum_required(VERSION 3.0.0)
project(Testing VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(Testing main.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

find_package(cpr CONFIG REQUIRED)
target_link_libraries(cpr PRIVATE cpr::cpr)

Full error output

[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_TOOLCHAIN_FILE:STRING=C:/tools/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET:STRING=x64-windows -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -Se:/programming/2022/cpp/Testing/cpp-vcpkg-cmake-example -Be:/programming/2022/cpp/Testing/cpp-vcpkg-cmake-example/build -G "Visual Studio 17 2022" -T host=x86 -A win32
[cmake] Not searching for unused variables given on the command line.
[cmake] -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
[cmake] CMake Error at C:/tools/vcpkg/scripts/buildsystems/vcpkg.cmake:826 (_find_package):
[cmake]   Could not find a configuration file for package "cpr" that is compatible
[cmake]   with requested version "".
[cmake] 
[cmake]   The following configuration files were considered but not accepted:
[cmake] 
[cmake]     C:/tools/vcpkg/installed/x64-windows/share/cpr/cprConfig.cmake, version: 1.9.0 (64bit)
[cmake] 
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:13 (find_package)
[cmake] 
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!
[cmake] See also "E:/programming/2022/cpp/Testing/cpp-vcpkg-cmake-example/build/CMakeFiles/CMakeOutput.log".

I have thought about adding adding specific version cpr to cmake but could not figure out how to do it, but that should not even be the underlying problem in this situation.

  • Are you building a 32 bit application? `C:/tools/vcpkg/installed/x64-windows` would be incompatible for that. Edit: `-T host=x86` yes you are! The bug is this: `-T host=x86` it must be `-T host=x64` for your 64 bit installed vcpkg packages. – drescherjm Aug 23 '22 at 00:54
  • I'll check soon if that was the error, I never looked at my cmake to check if it was the right version only the package version, thanks for the reply! – MagicalPineapple Aug 23 '22 at 01:16
  • Yes your solution does seem to fix the problem of not finding the correct version however now it is giving the following error. CMake Error at CMakeLists.txt:16 (target_link_libraries): Cannot specify link libraries for target "cpr" which is not built by this project. am I using target_link_libraries incorrectly? I used the one that vcpkg generated. – MagicalPineapple Aug 23 '22 at 14:40
  • 1
    `target_link_libraries(cpr PRIVATE cpr::cpr)` is a typo. Should be `target_link_libraries(Testing PRIVATE cpr::cpr)` – drescherjm Aug 23 '22 at 14:43
  • @drescherjm -- I think the issue is _also_ with `-A win32` which should be `-A x64`. The `-T` flag just specifies the arch of the compiler _itself_. Even 32-bit applications should generally be built with `-T host=x64` to avoid out-of-memory errors while linking. – Alex Reinking Aug 24 '22 at 13:49

1 Answers1

1

-DVCPKG_TARGET_TRIPLET:STRING=x64-windows

means x64-windows triplet/libraries will be used (->vcvars64)

-G "Visual Studio 17 2022"

means VS 2022 will be used (defaults to x64)

-T host=x86

means VS2022 x86 host tools will be used -> vcvars(32|x86_<?>)?

-A win32

means VS2022 will try to build for x86/win32 (overrides the default of x64). (-> vcvars32)

As such VCPKG_TARGET_TRIPLET=x64-windows is incompatible with -A win32. Do either not pass the -A flag to cmake or don't set VCPKG_TARGET_TRIPLET and let the vcpkg toolchain automatically select it. CMake is telling you that the libraries it found (x64) cannot be used for the targeted architecture (x86)

Alexander Neumann
  • 1,479
  • 8
  • 17