简体   繁体   中英

CMake: target_include_directories() can't find header files

At the top of my main.cpp file, No such file or directory is thrown at #include <sqlite3.h> .

Building the code manually by g++ -I"C:\\Libraries\\64_bit\\SQLite3\\include\\" -L"C:\\Libraries\\64_bit\\SQLite3\\bin\\" -lsqlite3 main.cpp Class1.cpp Class1.h Class2.cpp Class2.h -o main throws no errors.

CMake cannot seem to find the header, even though I've explicitly described where it is located in my file-system. According to the documentation for target_include_directories() , that should be enough:

Specified include directories may be absolute paths or relative paths. Repeated calls for the same append items in the order called.

Why is the target_include_directories() function not finding the headers, even though I've provided the exact absolute path?

I'm developing on a 64-bit Windows 10 machine, and CLion is set-up to compile with the MinGW-w64 g++ compiler.

  • I've downloaded the 64-bit Windows pre-compiled binary, sqlite3.dll and stored it locally in C:\\Libraries\\64_bit\\SQLite3\\bin\\ .
  • In order to access SQLite C++ functions I've also downloaded SQLite3's amalgamated source code and stored all source files in C:\\Libraries\\64_bit\\SQLite3\\include\\ .

I built my project in CLion, which is essentially a fancy GUI-wrapper for CMake. In my CMakeLists.txt , I've included SQLite3's headers and linked sqlite3 as follows:

cmake_minimum_required(VERSION 3.7)
project(My_Project)

set(CMAKE_CXX_STANDARD 11)

set(INCLUDE_DIRS C:\\Libraries\\64_bit\\SQLite3\\include\\)
set(LIBRARIES sqlite3.dll)
# My project's source code
set(SOURCE_FILES main.cpp Class1.cpp Class1.h Class2.cpp Class2.h)

add_executable(My_Project ${SOURCE_FILES})

# For compiler warnings
target_compile_options(My_Project PRIVATE -Wall)
# Including SQLite3's headers
target_include_directories(My_Project PRIVATE ${INCLUDE_DIRS})
# Linking against sqlite3.dll
target_link_libraries(My_Project ${LIBRARIES})

You can run into problems if you don't put paths between quotes.

Thus it is a good idea to write:

set(INCLUDE_DIRS "C:\\Libraries\\64_bit\\SQLite3\\include\\")

or, rather:

set(INCLUDE_DIRS "C:/Libraries/64_bit/SQLite3/include/")

Additionally, the CMakeLists.txt as it currently stands won't be able to find -lsqlite3 . Thankfully, CMake makes finding libraries easy:

# Optionally, add 'PATHS "C:/Libraries/64_bit/SQLite3/bin/"'
find_library(SQLITE3_LIBRARY NAMES sqlite3)

If the library is discover-able on your system, the above command will return the path to the library and store that path in SQLITE3_LIBRARY . All that remains to do is link the project against the SQLite3 library:

# Link project to the SQLite3 library
target_link_libraries(MSP_Tool ${SQLITE3_LIBRARY})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM