0

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.

Peter Mitrano
  • 2,132
  • 28
  • 31
  • 1
    You probably need some sub-folders in your project structure (see e.g. [here](https://stackoverflow.com/questions/2360734/whats-a-good-directory-structure-for-larger-c-projects-using-makefile)). What I've often seen if you use something like `#include "core/core.h"` that your have something reflecting this in your folder structure e.g. via `include/core/core.h`. – Florian May 27 '18 at 18:03
  • 1
    One more example: [The Ultimate Guide to Modern CMake](https://rix0r.nl/blog/2015/08/13/cmake-guide/) – Florian May 27 '18 at 18:14

0 Answers0