I'm trying to use modern cmake and I've run into the issue where all my #include
statements have no folders in them and it's confusing as all hell. Here's an example
Project structure:
root:
- CMakeLists.txt
- core
- CMakeLists.txt
- core.h
- core.cpp
- bin
- bin.cpp
- CMakeLists.txt
- other_dirs
- ...
Root CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(scratch)
add_subdirectory(core)
add_subdirectory(bin)
bin/CMakeLists.txt
add_executable(x2_cli x2_cli.cpp)
target_link_libraries(x2_cli core)
core/CMakeLists.txt
add_library(core core.cpp core.h)
target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
I would like to write my include statements like #include "core/core.h"
, but with this strategy I have to just write #include "core.h"
. I think this is very confusing, because you get no sense of what header files belong to what libraries!
Is there a way to fix this which doesn't also allow me to #include
things form other folders I'm not supposed to? For example #include other_dir/bad.h
ideally should fail at compile time. Do I need a different directory structure? Maybe there's a CMake way to do it?
Just writing target_include_directories(core PUBLIC ..)
is not good enough because it pollutes my include path, and it's misleading.