First, basic grammar

option(<variable> "<help_text>" [value])
Copy the code
  • Variable is the name of a variable.
  • Help_text is the description.
  • Value is the initial value of a variable and can only be ON or OFF.

Second, pay attention to

1. For value, if no value is given or other values are given, the default value is OFF

# CMake Minimum version requirements
cmake_minimum_required(VERSION 3.5)

# Project name
project(test_6)

# Default OFF for no or other values given
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}")
Copy the code

The result is:

OPTION_1 = OFF
OPTION_2 = OFF
Copy the code

2. In script processing, if variable has not been defined, then the statement before option is not defined for variable until option is executed. Then variable is really defined.

if(address)
    message("defined address!!!!!!!!!!")
else(a)message("NOT defined address!!!!!!!!!")
endif(a)option(address "hello world" ON)
message("option is ${address}")

if(address)
    message("defined address!!!!!!!!!!")
else(a)message("NOT defined address!!!!!!!!!")
endif(a)Copy the code

The result is:

NOT defined address!!!!!!!!!
option is ON
defined address!!!!!!!!!!
Copy the code

In practice, it can be used as a switch. For example:

# CMake Minimum version requirements
cmake_minimum_required(VERSION 3.5)

# Project name
project(test_6)

option(OPENGL_VIEW "Enable GLUT OpenGL point cloud view" ON)
if(OPENGL_VIEW)
	...
endif(a)Copy the code

If OPENGL_VIEW is ON, execute the code inside if, otherwise not.

Reference: cmake.org/cmake/help/… www.cnblogs.com/lidabo/p/73…