简体   繁体   中英

How to use include_directories correctly in cmake?

I have two cmake files:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)
project(as_math_engine)

set(CMAKE_CXX_STANDARD 14)

include_directories(include/as_math_engine)
add_library(as_math_engine evaluable.h)
add_subdirectory(tests)

tests/CMakeLists.txt:

include_directories(libs)
add_executable(as_math_engine_tests src/main.cpp)

And I have this include/as_math_engine/evaluable.h file but, CMake tells me:

Cannot find source file: evaluable.h

Why is it? And how can I solve this problem?

include_directories() is best used to set include paths for multiple targets within a project, target_include_directories() is usually preferred.

There are probably better ways of setting up as_math_engine if it is going to be a header-only library.

You also need to use add_library(as_math_engine include/as_math_engine/evaluable.h) because the add_library() doesn't search for files.

Header Only Library shows how to set this up and use it to avoid these kinds of problems.

Alternatively delete both include_directories() and use target_include_directories(as_math_engine_tests PRIVATE "${CMAKE_SOURCE_DIR}/include/as_math_engine" libs) so that the as_math_engine_tests uses the proper include path.

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