Overriding a default option(...) value in CMake from a parent CMakeLists.txt
Solution 1
Try setting the variable in the CACHE
SET(FOO_BUILD_SHARED OFF CACHE BOOL "Build libfoo shared library")
Note: You need to specify the variable type and a description so CMake knows how to display this entry in the GUI.
Solution 2
This question is rather old but Google brought me here.
The problem with SET(<variable name> <value> CACHE BOOL "" FORCE)
is that it will set the option project wide. If you want to use a sub-project, which is a library, and you want to set BUILD_STATIC_LIBS
for the sub-project (ParentLibrary
) using SET(... CACHE BOOL "" FORCE)
it will set the value for all projects.
I'm using the following project structure:
|CMakeLists.txt (root)
|- dependencies
| CMakeLists.txt (dependencies)
|- ParentLibrary
| CMakeLists.txt (parent)
|- lib
| CMakeLists.txt (lib)
Now I have CMakeLists.txt (dependencies)
which looks like this:
# Copy the option you want to change from ParentLibrary here
option (BUILD_SHARED_LIBS "Build shared libraries" ON)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(ParentLibrary)
Advantage is that I don't have to modify ParentLibrary
and that I can set the option only for that project.
It is necessary to explicitly copy the option
command from the ParentLibrary
as otherwise when executing CMake configuration initially the value of the variable would first be set by the set
command and later the value would be overwritten by the option
command because there was no value in the cache. When executing CMake configuration for the second time the option
command would be ignored because there is already a value in the cache and the value from the set
command would be used. This would lead to some strange behavior that the configuration between two CMake runs would be different.
Related videos on Youtube
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on July 05, 2022Comments
-
Admin almost 2 years
I am trying to include several third-party libraries in my source tree with minimal changes to their build system for ease of upgrading. They all use CMake, as do I, so in my own CMakeLists.txt I can use
add_subdirectory(extern/foo)
for libfoo.But the foo CMakeLists.txt compiles a test harness, builds documentation, a shared library which I don't need, and so on. The libfoo authors had the foresight to control these via options -
option(FOO_BUILD_SHARED "Build libfoo shared library" ON)
for example - which means I can set them via the CMake command line. But I would like to make that off by default and overridable via the command line.I have tried doing
set(FOO_BUILD_SHARED OFF)
beforeadd_subdirectory(extern/foo)
. That has the effect of not trying to build the shared library during the second and subsequent build attempts, but not during the first one, which is the one I really need to speed up.Is this possible, or do I need to maintain forked CMakeLists.txt for these projects?
-
tibur almost 14 yearsPut it before the add_subdirectory command, so it will set the default value for the variable.
-
asiviero about 10 yearsSometimes you need to use the
FORCE
-
Claudiu over 8 yearsPro tip (to myself):
FORCE
goes after the doc string, not randomly in the middle... -
Abai over 8 years@Rajish If the variable is already cached due to a previous
OPTION
orSET( CACHE )
call or a CMakeCache.txt from a previous configure, you have to useSET(FOO_BUILD_SHARED OFF CACHE BOOL "Build libfoo shared library" FORCE)
. source -
Zingam over 6 yearsWon't INTERNAL do?
-
stackprotector almost 4 yearsGood idea, but to be precise, the option will be valid for all sub-projects under dependencies, not only for one.
-
Top-Master almost 3 yearsUse
INTERNAL
instead ofBOOL
if want to hide option (from CMake GUI). Anyway, what aboutcmake_dependent_option
? Does this work for that as well?