How to set a CMake option() at command line

CmakeCmakelists Options

Cmake Problem Overview


I created a CMakeLists.txt that contains the following

project(P4V)
cmake_minimum_required(VERSION 2.6)

option(BUILD_STATIC_LIBS "Build the static library" ON)
option(BUILD_SHARED_LIBS "Build the shared library" ON)
option(BUILD_TESTS "Build test programs" OFF)

include_directories(${CMAKE_SOURCE_DIR}/include)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_BUILD_TYPE Release)

add_subdirectory(src)
if(BUILD_TESTS)
    add_subdirectory(tests)
endif(BUILD_TESTS)

By default BUILD_TESTS is OFF, how can I turn it ON without CMake GUI, but with cmake command itself? I tried the following, but it doesn't work

cmake .. -G %1 -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=ON -DBUILD_TESTS=ON

Cmake Solutions


Solution 1 - Cmake

Delete the CMakeCache.txt file and try this:

cmake -G %1 -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=ON -DBUILD_TESTS=ON ..

You have to enter all your command-line definitions before including the path.

Solution 2 - Cmake

An additional option is to go to your build folder and use the command ccmake .

This is like the GUI but terminal based. This obviously won't help with an installation script but at least it can be run without a UI.

The one warning I have is it won't let you generate sometimes when you have warnings. if that is the case, exit the interface and call cmake .

Solution 3 - Cmake

this works for me:

cmake -D DBUILD_SHARED_LIBS=ON DBUILD_STATIC_LIBS=ON DBUILD_TESTS=ON ..

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDenilson AmorimView Question on Stackoverflow
Solution 1 - Cmakefenix688View Answer on Stackoverflow
Solution 2 - Cmakecrazywill32View Answer on Stackoverflow
Solution 3 - CmakeMedici LorenzoView Answer on Stackoverflow