Home >Backend Development >C++ >How to Effectively Define Preprocessor Macros in CMake?
Defining Preprocessor Macros with CMake
Defining preprocessor variables with CMake has evolved over time. Let's explore how to do it effectively.
add_definitions
Previously, CMake primarily relied on the add_definitions command for this purpose. The equivalent C code would be #define foo.
add_definitions(OPENCV_VERSION=${OpenCV_VERSION} WITH_OPENCV2)
add_compile_definitions
However, starting with CMake version 3.12, the add_compile_definitions command offers a more refined approach. It specifically targets compile definitions, separating them from include directories and compiler options.
add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION}) add_compile_definitions(WITH_OPENCV2)
Benefits of add_compile_definitions
This newer approach eliminates the need for tricks often employed with add_definitions. It provides a more structured and error-prone way of managing compile definitions.
Target-Specific Definitions
As Jim Hunziker mentioned, you can also define preprocessor macros on a per-target basis. Refer to his answer for more details.
The above is the detailed content of How to Effectively Define Preprocessor Macros in CMake?. For more information, please follow other related articles on the PHP Chinese website!