samedi 25 juin 2016

CMake doesn't include header directory of submodule A within submodule B

I have a CMake project that looks like this: project/ CMakeLists.txt subprojectA/ CMakeLists.txt include/ headerA.hpp src/ libraryA.cpp subprojectB/ CMakeLists.txt src/ mainB.cpp The "library" subproject, A, is compiled as a static library, becoming libsubprojectA.a. The "main" project, B, is compiled as a binary and depends on the library. mainB.cpp includes a reference to headerA.hpp. Here is subprojectA/CMakeLists.txt: project(SubProjectA) include_directories(include) add_library(subprojectA STATIC src/libraryA.cpp) set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE) And here is subprojectB/CMakeLists.txt: project(SubProjectB) include_directories(${SubProjectA_INCLUDE_DIRS}) add_executable(mainBinary src/mainB.cpp) target_link_libraries(mainBinary subprojectA) The main Project CMakeLists.txt looks like: project(Project) add_subdirectory(subprojectB) add_subdirectory(subprojectA) Note that subprojectB, the main project, is listed before subprojectA. Here's the problem. When I first run "cmake" on this project, ${SubProjectA_INCLUDE_DIRS} is not set within SubProjectB. What I think is happening is that the CMakeLists for SubProjectB loads first, when ${SubProjectA_INCLUDE_DIRS} has not yet been set. It sets its own include path to an empty string as a result. However, even though libsubprojectA.a gets built successfully before mainBinary, the include path was already set empty beforehand. As a result, I get this error when trying to make mainBinary: subprojectB/src/mainB.cpp:1:23: fatal error: headerA.hpp: No such file or directory #include "headerA.hpp" ^ It's a workaround to put subprojectA before subprojectB in the main Project CMakeLists in the declarative world of CMake. What I really want is to know the proper way to indicate to CMake that the include_directories(${SubProjectA_INCLUDE_DIRS}) line depends on the definitions that exist inside SubProjectA's CMakeLists. Is there a better way to do this?

Aucun commentaire:

Enregistrer un commentaire