首页 > 解决方案 > 无法在 linux 上使用 vcpkg

问题描述

这是我的CMakelists.txt

cmake_minimum_required(VERSION 3.0) 
SET(CMAKE_TOOLCHAIN_FILE "/home/xxx/vcpkg/scripts/buildsystems/vcpkg.cmake")
project(test)

find_package(unofficial-sqlite3 CONFIG REQUIRED)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)

我正在尝试通过 vcpkg 使用SET(CMAKE_TOOLCHAIN_FILE ${vcpkg_root}),但似乎它不起作用。这是错误:

[cmake] CMake Error at CMakeLists.txt:5 (find_package):
[cmake]   Could not find a package configuration file provided by
[cmake]   "unofficial-sqlite3" with any of the following names:
[cmake] 
[cmake]     unofficial-sqlite3Config.cmake
[cmake]     unofficial-sqlite3-config.cmake

我已经验证unofficial-sqlite3-config.cmake确实存在于

/home/xxx/vcpkg/packages/sqlite3_x64-linux/share/unofficial-sqlite3/unofficial-sqlite3-config.cmake

我正在使用 WSL 和 vscode 作为 IDE,现在我不知道如何修复它。

标签: c++cmakewindows-subsystem-for-linux

解决方案


我无法使用以下基本 CMakeLists.txt 重现您的问题

cmake_minimum_required(VERSION 3.21)
project(test)

find_package(unofficial-sqlite3 REQUIRED)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)

我通过安装 sqlite3vcpkg install sqlite3并通过运行构建

$ echo 'int main() { return 0; }' > main.cpp
$ cmake -G Ninja -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build

如您所见,这完美无缺。我不确定将您的 CMake 策略级别设置为完全古老的3.0 是否重要,或者您是否使用旧的 vcpkg 结帐(我在撰写本文时使用了最近的提交)。


推荐阅读