CMake learning (seven) - option

First, the basic grammar

option(<variable> "<help_text>" [value])
  • variable is a variable name.
  • help_text is descriptive information.
  • the initial value is a variable value, can only be ON or OFF.

Second, attention

1. For the value, is not given or given default values ​​other OFF

# CMake最低版本要求
cmake_minimum_required(VERSION 3.5)

# 项目名称
project(test_6)

# 不给定或给定其他值都默认 OFF
option(OPTION_1 "Enable GLUT OpenGL point cloud view")
option(OPTION_2 "Enable GLUT OpenGL point cloud view" KKK)

message("OPTION_1 = ${OPTION_1}")
message("OPTION_2 = ${OPTION_2}")

The results are:

OPTION_1 = OFF
OPTION_2 = OFF

2. In the script process, if not defined variable, the statement before the option for the variable is not defined until the execution of the option, at this time only the true definition of a variable
, for example:

if(address)
    message("defined address!!!!!!!!!!")
else()
    message("NOT defined address!!!!!!!!!")
endif()

option(address "hello world" ON)
message("option is ${address}")

if(address)
    message("defined address!!!!!!!!!!")
else()
    message("NOT defined address!!!!!!!!!")
endif()

The results are:

NOT defined address!!!!!!!!!
option is ON
defined address!!!!!!!!!!

Practical use can be used as a switch
, such as:

# CMake最低版本要求
cmake_minimum_required(VERSION 3.5)

# 项目名称
project(test_6)

option(OPENGL_VIEW "Enable GLUT OpenGL point cloud view" ON)
if(OPENGL_VIEW)
	...
endif()

If OPENGL_VIEW is ON code inside the if is executed, or not executed.

Reference:
https://cmake.org/cmake/help/v3.14/command/option.html?highlight=option
https://www.cnblogs.com/lidabo/p/7359271.html

Published 72 original articles · 87 won praise · Views 200,000 +

Guess you like

Origin blog.csdn.net/maizousidemao/article/details/104103279