Greetings! This is my first post. I am new to writing CMake, and am currently updating a legacy build system. Assume a directory structure like this:
A
|-CMakeLists.txt
|-B
| |-CMakeLists.txt
| |-some_class_in_B.h
| |-some_class_in_B.cpp
|-C
| |-CMakeLists.txt
| |-some_class_in_C.h
| |-some_class_in_C.cpp
A/CMakeLists.txt looks something like
cmake_minimum_required(VERSION 3.11)
project(A)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/B)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/C)
B/CMakeLists.txt looks something like
project(B)
set(ProjectSources
B.cpp
)
set(ProjectHeaders
B.h
)
add_library(${PROJECT_NAME} SHARED ${ProjectSources} ${ProjectHeaders})
set(TargetLibraries C)
include_directories($ENV{PROJECT_INCLUDE})
target_link_libraries(${PROJECT_NAME} ${TargetLibraries})
B’s implementation file and header file both look something like this
//header
#IFNDEF SOME_CLASS_IN_B_H
#DEFINE SOME_CLASS_IN_B_H
#include "C/some_class_in_C.h"
class someClassInB
{
...
}
#ENDIF
//implementation
#include "C/some_class_in_C.h"
void someClassInB::method()
{
...
}
The PROJECT_INCLUDE environment variable is set as the install target for every header throughout the project. I’m unsure if this is best practice or not, but I’d like to move the project towards using the target_sources command more than the include_directories command. I currently have target_sources working for some subdirectories of the project, but they erase the ability to use the C/some_class_in_C.h include structure. Instead I’m having to use #include some_class_in_C.h, which makes it more vague where the included header is located.
An example of what the updated CMakeLists look like is like this:
project(B)
set(ProjectSources
B.cpp
)
set(ProjectHeaders
B.h
)
add_library("${PROJECT_NAME}" SHARED)
target_sources("${PROJECT_NAME}"
PRIVATE "${ProjectSources}"
PUBLIC
FILE_SET HEADERS
BASE_DIRS "${PROJECT_SOURCE_DIR}"
FILES "${ProjectHeaders}"
)
target_link_libraries("${PROJECT_NAME}" PRIVATE C)
But then that requires changes in the C++ from #include C/some_class_in_C.h to #include some_class_in_C.h. I’d prefer to keep the context of where the included header is coming from, but get build errors when I try building with the previous C++ include statement.
I can’t post the source code for the project itself, but I’ll do my best to clarify any questions anyone has. Essentially my question boils down to "is there a way to preserve the C++ include statements the way they were while also reaping the benefits of the more modern, simplified and robust CMake syntax?