在 windows 上设置 CMake 和 vcpkg 时出错`无法打开包含文件`

Errors setting up CMake and vcpkg on windows `Can't open include file`

我一直在尝试遵循 vcpkg setup guide 但我一直无法解决在最后一步遇到的构建错误:

fatal error C1083: Cannot open include file: 'sqlite3.h': No such file or directory

我的vcpkg设置过程如下:

我确认 sqlite 安装:

> vcpkg list

sqlite3:x64-windows                 3.29.0-1         SQLite is a software library that implements a s...

我在测试仓库中创建了以下文件:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(test)

find_package(Sqlite3 REQUIRED)

add_executable(main main.cpp)
target_link_libraries(main sqlite3)

main.cpp:

#include <sqlite3.h>
#include <stdio.h>

int main()
{
    printf("%s\n", sqlite3_libversion());
    return 0;
}

开始构建:

然后我通过 运行 构建文件夹中的以下内容生成构建文件,并注意它 能够找到 SQLite3:

> cmake .. -DCMAKE_TOOLCHAIN_FILE=C:/Workspace/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows

-- Building for: Visual Studio 16 2019
-- Using toolchain file: C:/Workspace/vcpkg/scripts/buildsystems/vcpkg.cmake
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.15063.
-- The C compiler identification is MSVC 19.23.28106.4
-- The CXX compiler identification is MSVC 19.23.28106.4
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found SQLite3: C:/Workspace/vcpkg/installed/x64-windows/include (found version "3.29.0")
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Workspace/testCmake/build

最后,我触发了构建,但失败了:

> cmake --build .

Microsoft (R) Build Engine version 16.3.1+1def00d3d for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  Checking Build System
  Building Custom Rule C:/Workspace/testCmake/CMakeLists.txt
  main.cpp
C:\Workspace\testCmake\main.cpp(1,10): fatal error C1083: Cannot open include file: 'sqlite3.h': No such file or directory [C:\Workspace\testCmake\build\main.vcxproj]

当您使用 target_link_libraries() 为 linking 提供 SQLite3 库时,您需要让您的可执行文件知道在哪里可以找到头文件。使用 target_include_directories():

cmake_minimum_required(VERSION 3.0)
project(test)

find_package(Sqlite3 REQUIRED)

add_executable(main main.cpp)

# Add this line after the target is defined.
target_include_directories(main PRIVATE ${SQLite3_INCLUDE_DIRS})

target_link_libraries(main sqlite3)

如果找到 SQLite3(看起来确实如此),CMake 将填充这些变量:

  • SQLite3_INCLUDE_DIRS:在哪里可以找到sqlite3.h,等等
  • SQLite3_LIBRARIES:link 反对使用 SQLite3 的库。
  • SQLite3_VERSION:找到的 SQLite3 库版本
  • SQLite3_FOUNDTRUE 如果找到

因此,如果您对 target_link_libraries() 的调用没有按预期工作,请尝试使用 ${SQLite3_LIBRARIES} 而不是 sqlite3