Consider a C++ project, organized in a git repository. Assume that the git repository has a submodule from which a library is built on which the (super)project depends. If the (super)project depends not only on the library but on the library built with specific (CMake) parameters, how can it be ensured that the submodule is built with these parameters when the (super)project is built?
-
I'm sorry to get a bit off topic here, but this is the reason I try to avoid CMake if I can. Last I looked there is exactly one book on writing CMake files. There are a 5 bajillion on Make and autotools. If the books skips the topic you are SOL. – TLOlczyk Jul 22 '15 at 15:31
-
Is your sub-module built separately or as part of your super-project? – nils Jul 22 '15 at 15:36
-
The submodule is a library that is built separately and then linked to an executable of the (super)project. – Max Flow Jul 22 '15 at 20:23
-
@TLOlczyk: well, then how'd you do it with autotools? – tamas.kenez Jul 23 '15 at 02:06
4 Answers
The build options (like MYLIB_WITH_SQLITE
) must be added to the interface of the library, that is, to the MYLIB_DEFINITIONS
variable in case of an old-school config-module, or to the INTERFACE_COMPILE_DEFINITIONS
property, if the library creates its config-module with 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 consuming library or executable you can write simple compile-time checks:
#ifndef MYLIB_WITH_SQLITE
#error mylib must be built with sqlite
#endif

- 7,301
- 4
- 24
- 34
CMake supports external projects wiht ExternalProject_Add
where you can define compile flags anr more to your liking. Use it to build your submodules.
More details: https://cmake.org/cmake/help/v3.2/module/ExternalProject.html

- 2,343
- 1
- 32
- 49
I think the simplest answer is to use the -C
option as described in this SO answer.
-
I am not happy with the solution. Firstly, it is left to the user to call cmake with the right parameters. Secondly, it is the CMake call for the main project, not the submodule. – Max Flow Jul 31 '15 at 13:48
-
I am not sure what you are trying to do. The ``-C`` option seems reasonable when you have some custom settings you need to use. The other choices seem to be using the cmake ExternalProject module or scripting with a ``-P`` cmake script. – Phil Jul 31 '15 at 14:06
One solution I consider is to fork the submodule's repo, edit the CMakeLists.txt so as to be more specific, and to then include the HEAD revision of the forked repo as a submodule. Inelegant but acceptable.

- 607
- 7
- 15