我在 Windows 中使用 Clion 和默认 CMake。我想使用第三方库,例如 sqlite3 或 openSSL。我完成了this教程。看起来真的很容易,但事实并非如此。我有一个 CMake 问题..我配置了 cmake 命令:
C:\Users\User\AppData\Local\Programs\CLion\bin\cmake\win\x64\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=C:/Users/User/AppData/Local/Programs/CLion/bin/ninja/win/x64/ninja.exe -DVCPKG_TARGET_TRIPLET=x64-windows -DCMAKE_TOOLCHAIN_FILE=C:\Users\User\.vcpkg-clion\vcpkg\scripts\buildsystems\vcpkg.cmake -G Ninja -S C:\Users\User\CLionProjects\unified -B C:\Users\User\CLionProjects\unified\cmake-build-debug
我的 CMake 文件:
cmake_minimum_required(VERSION 3.28)
project(project)
set(CMAKE_CXX_STANDARD 26)
find_package(unofficial-sqlite3 CONFIG REQUIRED)
add_executable(project main.cpp)
target_link_libraries(project PRIVATE unofficial::sqlite3::sqlite3)
和错误=)
CMake Error at C:/Users/User/.vcpkg-clion/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package):
Could not find a package configuration file provided by
"unofficial-sqlite3" with any of the following names:
unofficial-sqlite3Config.cmake
unofficial-sqlite3-config.cmake
Add the installation prefix of "unofficial-sqlite3" to CMAKE_PREFIX_PATH or
set "unofficial-sqlite3_DIR" to a directory containing one of the above
files. If "unofficial-sqlite3" provides a separate development package or
SDK, be sure it has been installed.
我该如何修复它?
附加问题:你能推荐我如何轻松安装第三方 C++ 库吗?在 Linux 上,这非常简单。 Cmake 文件中的一条命令+一行。但在 Windows 上.. 上周我尝试了 winget、官方网站,最后一次尝试是 vcpkg。没什么帮助=(
您在 CMake 中找不到
unofficial-sqlite3
软件包所面临的问题可能源于未通过 vcpkg 安装库或 CMake 配置未指向已安装软件包的正确位置。
确保通过 vcpkg 安装库:
unofficial-sqlite3
是使用vcpkg安装的。您可以通过运行以下命令来安装它:
vcpkg install unofficial-sqlite3:x64-windows
CMake配置:
CMake
命令看起来不错,但请确保 CMAKE_TOOLCHAIN_FILE
正确指向 vcpkg.cmake
文件。CMAKE_PREFIX_PATH
以明确指向 vcpkg 安装包的位置:
-DCMAKE_PREFIX_PATH=C:/Users/User/.vcpkg-clion/vcpkg/installed/x64-windows
检查CMakeLists.txt:
CMakeLists.txt
文件设置正确以查找包:
cmake_minimum_required(VERSION 3.28)
project(project)
set(CMAKE_CXX_STANDARD 26)
# Include vcpkg toolchain file
set(CMAKE_TOOLCHAIN_FILE "C:/Users/User/.vcpkg-clion/vcpkg/scripts/buildsystems/vcpkg.cmake")
# Find SQLite3
find_package(unofficial-sqlite3 CONFIG REQUIRED)
add_executable(project main.cpp)
target_link_libraries(project PRIVATE unofficial::sqlite3::sqlite3)
vcpkg:
vcpkg
安装库后,请确保使用正确的三元组(如 x64-windows
)并添加适当的 CMake
配置。CMake 的 FetchContent:
FetchContent
是在配置时下载和构建依赖项的好方法:
include(FetchContent)
FetchContent_Declare(
sqlite3
GIT_REPOSITORY https://github.com/sqlite/sqlite.git
GIT_TAG version_number # Replace with a specific version
)
FetchContent_MakeAvailable(sqlite3)
add_executable(project main.cpp)
target_link_libraries(project PRIVATE sqlite3)
手动安装:
CMAKE_PREFIX_PATH
设置为安装目录。您还需要在 CMake 中手动设置包含目录和链接库。使用柯南: