[英]Errors setting up CMake and vcpkg on windows `Can't open include file`
I've been trying to follow the vcpkg setup guide but I've been unable to solve the build error I get on the final step:我一直在尝试遵循vcpkg 设置指南,但我无法解决我在最后一步遇到的构建错误:
fatal error C1083: Cannot open include file: 'sqlite3.h': No such file or directory
My vcpkg setup process is as follows:我的 vcpkg 设置过程如下:
> git clone https://github.com/Microsoft/vcpkg.git
> cd vcpkg\
>.\bootstrap-vcpkg.bat
> vcpkg install sqlite3:x64-windows
> vcpkg integrate install
I confirm the sqlite install:我确认 sqlite 安装:
> vcpkg list
sqlite3:x64-windows 3.29.0-1 SQLite is a software library that implements a s...
I create the following files in a test repo:我在测试存储库中创建以下文件:
CMakeLists.txt: 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:主.cpp:
#include <sqlite3.h>
#include <stdio.h>
int main()
{
printf("%s\n", sqlite3_libversion());
return 0;
}
Start Build:开始构建:
I then generate the build files by running the following in the build folder, and note that it is able to find SQLite3
:然后我通过在构建文件夹中运行以下命令来生成构建文件,并注意它能够找到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
Lastly, I trigger the build, which fails:最后,我触发了构建,但失败了:
> 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]
While you provide the SQLite3 library for linking with target_link_libraries()
, you need to let your executable know where to find the header files.当您提供用于与target_link_libraries()
链接的 SQLite3 库时,您需要让您的可执行文件知道在哪里可以找到 header 文件。 Use target_include_directories()
:使用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)
CMake will populate these variables if SQLite3 is found (which it looks like it is):如果找到 SQLite3(看起来是这样),CMake 将填充这些变量:
SQLite3_INCLUDE_DIRS
: where to find sqlite3.h, etc. SQLite3_INCLUDE_DIRS
:在哪里可以找到 sqlite3.h 等。SQLite3_LIBRARIES
: the libraries to link against to use SQLite3. SQLite3_LIBRARIES
:链接到使用 SQLite3 的库。SQLite3_VERSION
: version of the SQLite3 library found SQLite3_VERSION
:找到的 SQLite3 库的版本SQLite3_FOUND
: TRUE
if found SQLite3_FOUND
:如果找到则为TRUE
So if your call to target_link_libraries()
doesn't work as expected, try using ${SQLite3_LIBRARIES}
instead of sqlite3
.因此,如果您对target_link_libraries()
的调用没有按预期工作,请尝试使用${SQLite3_LIBRARIES}
而不是sqlite3
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.