CMake无法找到unofficial-sqlite3包配置文件问题求助
解决CMake + vcpkg依赖查找失败问题
问题背景
使用Visual Studio打开Setup目录生成CMake项目时,出现unofficial-sqlite3等依赖找不到的错误,手动添加HINTS参数后仍未解决。
初始错误日志
1> [CMake] -- Running vcpkg install - done 1> [CMake] -- Could NOT find unofficial-sqlite3 (missing: unofficial-sqlite3_DIR) 1> [CMake] CMake Error at C:/Users/user1/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package): 1> [CMake] Could not find a package configuration file provided by "unofficial-sqlite3" with any of 1> [CMake] the following names: 1> [CMake] 1> [CMake] unofficial-sqlite3Config.cmake 1> [CMake] unofficial-sqlite3-config.cmake 1> [CMake] 1> [CMake] Add the installation prefix of "unofficial-sqlite3" to CMAKE_PREFIX_PATH or set 1> [CMake] "unofficial-sqlite3_DIR" to a directory containing one of the above files. If "unofficial-sqlite3" 1> [CMake] provides a separate development package or SDK, be sure it has been 1> [CMake] installed. 1> [CMake] Call Stack (most recent call first): 1> [CMake] src/CMakeLists.txt:6 (find_package) 1> [CMake] -- Configuring incomplete, errors occurred!
项目文件夹结构
Setup/ build/ src/ CMakeLists.txt main.cpp CMakeLists.txt vcpkg.json
配置文件内容
vcpkg.json
{ "name": "whatever", "version-string": "whatever", "dependencies": [ "fltk", "protobuf", "sqlite3", "boost" ], "builtin-baseline": "6f1ddd6b6878e7e66fcc35c65ba1d8feec2e01f8" }
顶层CMakeLists.txt
cmake_minimum_required(VERSION 3.29) set(CMAKE_TOOLCHAIN_FILE "C:/Users/user1/vcpkg/scripts/buildsystems/vcpkg.cmake") project( NETWORKING VERSION 0.00 LANGUAGES CXX ) add_subdirectory(src)
src/CMakeLists.txt(初始版本)
add_executable(main main.cpp) message("DIR IS: ${CMAKE_SOURCE_DIR}") find_package(unofficial-sqlite3 HINTS ${CMAKE_SOURCE_DIR}/build/x64-Debug/vcpkg_installed/x64-windows/ CONFIG REQUIRED) find_package(FLTK HINTS ${CMAKE_SOURCE_DIR}/build/x64-Debug/vcpkg_installed/x64-windows/ CONFIG REQUIRED ) find_package(protobuf HINTS ${CMAKE_SOURCE_DIR}/build/x64-Debug/vcpkg_installed/x64-windows/ CONFIG REQUIRED) target_link_libraries(main PRIVATE fltk fltk_gl fltk_forms fltk_images) target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite) target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
添加HINTS后的错误日志
-- Running vcpkg install - done 1> [CMake] DIR IS: C:/Users/user1/source/repos/NewSetu[ 1> [CMake] -- Could NOT find unofficial-sqlite3 (missing: unofficial-sqlite3_DIR) 1> [CMake] -- Could NOT find FLTK (missing: FLTK_DIR) 1> [CMake] -- Could NOT find protobuf (missing: protobuf_DIR) 1> [CMake] -- Configuring done (3.1s) 1> [CMake] CMake Error at src/CMakeLists.txt:10 (target_link_libraries): 1> [CMake] Target "main" links to: 1> [CMake] 1> [CMake] protobuf::libprotoc 1> [CMake] 1> [CMake] but the target was not found. Possible reasons include: 1> [CMake] 1> [CMake] * There is a typo in the target name. 1> [CMake] * A find_package call is missing for an IMPORTED target. 1> [CMake] * An ALIAS target is missing. 1> [CMake] 1> [CMake] 1> [CMake] 1> [CMake] -- Generating done (0.0s) 1> [CMake] CMake Generate step failed. Build files cannot be regenerated correctly.
解决方案
1. 移除硬编码HINTS路径并修正链接目标
vcpkg的toolchain文件会自动处理依赖查找路径,手动添加HINTS会干扰正常逻辑,且硬编码路径仅适配特定构建配置。修改src/CMakeLists.txt如下:
add_executable(main main.cpp) # 移除HINTS参数,让vcpkg自动处理路径 find_package(unofficial-sqlite3 CONFIG REQUIRED) find_package(FLTK CONFIG REQUIRED) find_package(protobuf CONFIG REQUIRED) # 移除protobuf::libprotoc(仅用于编译.proto文件,应用程序无需链接) target_link_libraries(main PRIVATE fltk fltk_gl fltk_forms fltk_images protobuf::libprotobuf protobuf::libprotobuf-lite unofficial::sqlite3::sqlite3 )
2. 确认vcpkg集成有效性
- 打开Visual Studio的
工具->选项->CMake->常规,勾选使用vcpkg环境,并确认vcpkg路径指向C:/Users/user1/vcpkg。 - 在项目根目录终端执行
vcpkg install --triplet x64-windows,确保依赖完整安装。
3. 清理构建缓存
删除build目录下所有内容,重新在Visual Studio中生成CMake项目,避免旧缓存干扰。
4. 验证依赖配置文件
检查vcpkg安装目录下是否存在对应包的配置文件:
- unofficial-sqlite3:
C:/Users/user1/vcpkg/installed/x64-windows/share/unofficial-sqlite3/unofficial-sqlite3Config.cmake - FLTK:
C:/Users/user1/vcpkg/installed/x64-windows/share/fltk/FLTKConfig.cmake - protobuf:
C:/Users/user1/vcpkg/installed/x64-windows/share/protobuf/protobuf-config.cmake
若文件不存在,执行vcpkg remove --outdated后重新安装依赖。
内容的提问来源于stack exchange,提问作者sea
相关产品推荐
相关产品推荐

