If I have a folder-structure like this:
.
├── Core
│ ├── Private
│ │ └── example.cpp
│ └── Public
│ └── example.h
└── Math
├── Private
│ └── Math.cpp
└── Public
└── Math.h
How would I accomplish it, in CMake, to make a import, in example the folder Math/Private, like this:
#include <Core/example.h>
Of course example.h is the header file from example.cpp. Even there, how would I link them without writing:
#include <../Public/example.h>
Is this possible?
CodePudding user response:
You cannot accompilish this with the given project structure: There simply is no example.h
file with a parent directory Core
.
Restructure the project and use target_include_directories()
.
Project structure
.
├── Core
│ ├── Private
│ │ └── example.cpp
│ └── include
│ └── Core
│ └── example.h
├── Math
│ ├── Private
│ │ └── Math.cpp
│ └── include
│ └── Math
│ └── Math.h
└── CMakeLists.txt
CMakeLists.txt
set(CORE_SRC
Private/example.cpp
include/Core/example.h
)
list(TRANSFORM CORE_SRC PREPEND Core/)
set(MATH_SRC
Private/Math.cpp
include/Math/Math.h
)
list(TRANSFORM MATH_SRC PREPEND Math/)
add_executable(my_program
${CORE_SRC}
${MATH_SRC}
)
target_include_directories(my_program PRIVATE
Math/include
Core/include
)
Note: If you want to create separate targets for the components Math
and Core
, you could use PUBLIC
instead of PRIVATE
for the visibility of the include directories which would result in any cmake targets linking your cmake library targets to automatically gain access to those include directories without the need to repeat this kind of in multiple places.