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).

starball
  • 20,030
  • 7
  • 43
  • 238
Pandawan
  • 117
  • 3
  • 11
  • 3
    "I would like to be able to write `#include ` instead" - For that, you need to create `myproject` subdirectory and place the header `logger.h` into it. – Tsyvarev Apr 10 '23 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 `` nor `` work. – Pandawan Apr 10 '23 at 22:57
  • 2
    If you want to include the header via ``, 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})` – Tsyvarev Apr 10 '23 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 `` – Pandawan Apr 10 '23 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](https://api.csswg.org/bikeshed/?force=1&url=https://raw.githubusercontent.com/vector-of-bool/pitchfork/develop/data/spec.bs#tld.src) and my post [here](https://stackoverflow.com/a/75093720/11107541) – starball Apr 10 '23 at 23:59
  • @user I guess I'm coming from web dev world where that kind of thing is considered [absolute paths](https://stackoverflow.com/questions/55916731/using-absolute-paths-in-typescript-for-imports) (whereas anything relative to the current file is considered relative). My bad for using wrong terminology. And thanks for the resources, I'll look into those. – Pandawan Apr 11 '23 at 00:12

0 Answers0