CMake: override subdirectory options

Last year I moved Monkey build system to CMake and when I started Fluent Bit I did it in the same way, a great experience.

Due to the nature of the projects mentioned where they can be build as libraries, I found that overriding the sub directories options was not too straightforward: if I have a project A with some options they should be listed like this in the CMakeLists.txt file:

option(WITH_FEATURE_AA  "Enable feature AA"   ON)
option(WITH_FEATURE_BB  "Enable feature BB"   OFF)

Now if I started a project B and I have included project A in a subdirectory, the files hierarchy should be:


When building project_B based on it CMakeLists.txt rules, I may want to override some default options values of project_A, documentation is a bit confusing and you are likely to waste time trying to determinate why you cannot override the values, the guilty is the CMake Cache. As a workaround I found that building a simple macro in project_B/CMakeLists.txt solves the problem without complexity:

macro(SET_OPTION option value)
  set(${option} ${value} CACHE INTERNAL "" FORCE)

# Let's override the values

# Now join the subdirectory

If you want to see a working code example, checkout the following GIT repository:


  1. There might be a small mistake in your script. According to the doc of CMake, the syntax of “set” is :

    set(<variable> <value> [[CACHE <type> <docstring> [FORCE]] | PARENT_SCOPE])

    I think you might want to write:

    set(${option} ${value} CACHE "" INTERNAL FORCE)

    instead of :

    set(${option} ${value} CACHE INTERNAL "" FORCE)

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see