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