C ++ with git and CMake: how to build submodules with specific parameters?

Consider a C ++ project organized in a git repository. Suppose a git repository has a submodule from which the library is built on which a (super) project depends. If a project (super) depends not only on a library, but also on a library built with specific parameters (CMake), how can one ensure that a submodule is built with these parameters when building a (super) project?

+5
source share
4 answers

Assembly parameters (for example, MYLIB_WITH_SQLITE ) should be added to the library interface, that is, to the MYLIB_DEFINITIONS variable in the case of the old-school configuration module or to the INTERFACE_COMPILE_DEFINITIONS property if the library creates its own configuration module using the install(EXPORT ...) command:

 add_library(mylib ...) if(MYLIB_WITH_SQLITE) target_compile_definitions(mylib PUBLIC MYLIB_WITH_SQLITE) endif() ... install(TARGETS mylib EXPORT mylib-targets ...) install(EXPORT mylib-targets ...) 

And in the consumption library or executable you can write simple compile-time checks:

 #ifndef MYLIB_WITH_SQLITE #error mylib must be built with sqlite #endif 
+1
source

CMake supports wiht ExternalProject_Add external projects, where you can also define compilation flags to your liking. Use it to create your submodules.

More details: https://cmake.org/cmake/help/v3.2/module/ExternalProject.html

0
source

I think the easiest answer is to use the -C option as described in this SO answer .

-1
source

One solution that I am considering is to develop a submodule repository, edit CMakeLists.txt to be more specific, and then enable the HEAD revision of the forked repo as a submodule. Inelegant but acceptable.

-1
source

All Articles