0

I'm struggling with setting up CMake to support proper include paths.

I have a repository in this format:

root/
├── src/
│   ├── helpers/
│   │   ├── helper.h
│   │   ├── helper.cpp
│   │   └── CMakeLists.txt
│   ├── logger.h
│   ├── logger.cpp
│   ├── main.cpp
│   └── CMakeLists.txt
└── CMakeLists.txt

(I'm new to CMake and haven't made larger-sized C/C++ projects before, so this may not be a good way to organize things).

I am trying to setup CMake to create an executable from this project. I am able to compile everything properly; however, I'd like to be able to include things using paths relative to a path in the list of include directories.

For example, in my helper.h, I have #include "../logger.h" but I would like to be able to write #include <myproject/logger.h> instead (or something of the sort).

Here is my top level CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)
project(
    myproject
    VERSION 1.0.0
    LANGUAGES CXX C
)

add_executable(${PROJECT_NAME})

add_subdirectory(src)

Here is my src/CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)

target_sources(${PROJECT_NAME} PRIVATE 
    main.cpp
    logger.cpp
)

add_subdirectory(helpers)

And here is my src/helpers/CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)

target_sources(${PROJECT_NAME} PRIVATE 
    helper.cpp
)

I thought of adding target_include_directories to each level for their current directory but that did not change anything.

(Also, let me know if there are better ways to setup the project as a whole).

6
  • 3
    "I would like to be able to write #include <myproject/logger.h> instead" - For that, you need to create myproject subdirectory and place the header logger.h into it. Commented Apr 10, 2023 at 22:55
  • @Tsyvarev Okay so if I wanted to maintain logger.h inside src, what would be the appropriate way to include it? Neither <src/logger.h> nor <logger.h> work. Commented Apr 10, 2023 at 22:57
  • 2
    If you want to include the header via <src/logger.h>, then you need to specify the project's root directory as include one. Either target_include_directories(myproject ${CMAKE_SOURCE_DIR}) or include_directories(${CMAKE_SOURCE_DIR}) Commented Apr 10, 2023 at 23:33
  • Okay, that seems to work for helpers! One last question, how would I make it work so one subdirectory (e.g. "math") can include from another (e.g. "helpers") without having to go through src? Something like <helpers/helper.h> Commented Apr 10, 2023 at 23:47
  • 1
    "I'm new to CMake and haven't made larger-sized C/C++ projects before, so this may not be a good way to organize things" There are different opinions on this. One is: Do whatever you find works best for you. The other is: Do what most people are doing. For the latter, see the Pitchfork Layout Spec and my post here Commented Apr 10, 2023 at 23:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.