Defining Preprocessor Macros in CMake
Often, developers require preprocessor macros to be set for their CMake projects. Traditionally, CMake provided the add_definitions command for this purpose. However, since CMake version 3.12, a new approach has been introduced with separate commands for compile definitions, include directories, and compiler options.
Using the New Approach
To define a preprocessor macro using the new syntax, use the add_compile_definitions command as follows:
add_compile_definitions(MACRO_NAME=VALUE)
For example, to define the OPENCV_VERSION macro, you would write:
add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION})
You can also define multiple macros in a single command:
add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION} WITH_OPENCV2)
Advantages
This new approach provides several advantages over the add_definitions command:
Note: As noted in the documentation, it is not recommended to use the add_definitions command anymore.
The above is the detailed content of How to Define Preprocessor Macros in CMake Using the Recommended Approach?. For more information, please follow other related articles on the PHP Chinese website!