Search code examples
c++sqlitecmakevcpkg

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:

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

My vcpkg setup process is as follows:

  • > 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:

> 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:

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;
}

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:

> 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]

Solution

  • 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. Use 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_INCLUDE_DIRS: where to find sqlite3.h, etc.
    • SQLite3_LIBRARIES: the libraries to link against to use SQLite3.
    • SQLite3_VERSION: version of the SQLite3 library found
    • SQLite3_FOUND: TRUE if found

    So if your call to target_link_libraries() doesn't work as expected, try using ${SQLite3_LIBRARIES} instead of sqlite3.