Reputation: 2099
I have projectB as a subproject of projectA. Both projects contain an include directory for storing header libraries. The folder structure looks like this:
projectA/CMakeLists.txt
projectA/A.cpp
projectA/include/projectB/CMakeLists.txt
projectA/include/projectB/B.h
projectA/include/projectB/include/boost
projectA/include/projectB/include/...
Inside B.h, I am using Boost, so the include line looks like this:
#include <boost/algorithm/string.hpp>
In A.cpp, I am including B.h.
#include <B.h>
In projectA/CMakeLists.txt, I am including projectB and all of its includes like this:
include_directories(
include/projectB
include/projectB/include/boost
include/projectB/include/...)
If I didn't include all library directories that projectB uses, I cannot compile, as the compiler cannot find boost. (I get an error which says: boost/algorithm/string.hpp: No such file or directory
)
Is there a simpler process to have projectA import all includes from projectB and change the paths to reflect this project hierarchy?
Upvotes: 0
Views: 931
Reputation: 5800
With modern CMake (i.e. 3.x) it is not recommended any more to use include_directories
. The alternative is to define the include directories per target with appropriate "visibility".
Assuming, projectA
is a static or shared library defined in projectA/CMakeLists.txt
as follows, with projectA_SOURCES
being a list of all source files (i.e. not header files) for that library.
add_library(projectA ${projectA_SOURCES})
target_include_directories(projectA
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include/projectB/include
${CMAKE_CURRENT_LIST_DIR}/include/projectB/include/boost
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include/projectB)
Then, in projectB/CMakeLists.txt
after having target projectB
being defined (either as a library or executable, that does not matter):
target_include_directories(projectB
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include/projectA/include
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include/projectA)
target_link_library(projectB
PUBLIC projectA)
Upvotes: 2