I am currently using add_subdirectory(${CMAKE_SOURCE_DIR}/ext/glfw)
to add glfw to my project.
The problem is that once I do cmake .. && make it always builds all the examples and I don't want that to happen.
In the cmake file from glfw there is
option(GLFW_BUILD_EXAMPLES "Build the GLFW example programs" ON)
Is it possible to set this variable to OFF
from my CMakeLists.txt?
Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.
Options and variables are defined on the CMake command line like this: $ cmake -DVARIABLE=value path/to/source You can set a variable after the initial `CMake` invocation to change its value. You can also undefine a variable: $ cmake -UVARIABLE path/to/source Variables are stored in the `CMake` cache.
The CMake cache may be thought of as a configuration file. The first time CMake is run on a project, it produces a CMakeCache. txt file in the top directory of the build tree. CMake uses this file to store a set of global cache variables, whose values persist across multiple runs within a project build tree.
Simply do either
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "Build the GLFW example programs")
before the add_subdirectory
command or
cmake -DGLFW_BUILD_EXAMPLES:BOOL=OFF ..
on the command line
For example given the following two CMake scripts
cmake_minimum_required(VERSION 2.8)
project(test)
set(OVERRIDE FALSE CACHE BOOL "")
if(OVERRIDE)
message(STATUS "Overriding option")
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "Build the GLFW example programs")
endif()
message(STATUS "OPT BEFORE =${GLFW_BUILD_EXAMPLES}")
add_subdirectory(subdir)
message(STATUS "OPT AFTER=${GLFW_BUILD_EXAMPLES}")
and subdir/CMakeLists.txt
option(GLFW_BUILD_EXAMPLES "Build the GLFW example programs" ON)
you see the following (when run in the build directory
With no command line options, the option()
command sets the variable
$ rm -rf *
$ cmake ..
...
-- OPT BEFORE =
-- OPT AFTER=ON
-- Configuring done
-- Generating done
Turning on the over-ride, the set()
command overrides the option()
command
$ rm -rf *
$ cmake .. -DOVERRIDE=ON
...
-- Overriding option
-- OPT BEFORE =OFF
-- OPT AFTER=OFF
-- Configuring done
-- Generating done
You can also over-ride the option()
directly on the command line.
$ rm -rf *
$ cmake .. -DGLFW_BUILD_EXAMPLES=OFF
...
-- OPT BEFORE =OFF
-- OPT AFTER=OFF
-- Configuring done
-- Generating done
Note that I do an rm -rf *
in the build directory each time. The value of GLFW_BUILD_EXAMPLES
is cached in a file called CMakeCache.txt
. CMake will always use the values it finds there before anything set in a set
or option
command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With