Home >Backend Development >C++ >How to Define Preprocessor Macros in CMake?
In C , preprocessor macros provide a convenient way to define symbolic constants or alter code behavior during compilation. To define a preprocessor macro using CMake, you have two main options:
Method 1: add_compile_definitions (CMake >= 3.12)
With CMake 3.12 and later, you can use the add_compile_definitions command to define preprocessor macros. This approach is more fine-grained than the previous method and allows you to separate compile definitions, include directories, and compiler options.
add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION}) add_compile_definitions(WITH_OPENCV2)
Method 2: Legacy add_definitions (Deprecated)
Prior to CMake 3.12, the add_definitions command was used to define preprocessor macros:
add_definitions(-DOPENCV_VERSION=${OpenCV_VERSION}) add_definitions(-DWITH_OPENCV2)
Note: The -D prefix is required in the legacy method to indicate a preprocessor definition.
Additional Considerations:
The above is the detailed content of How to Define Preprocessor Macros in CMake?. For more information, please follow other related articles on the PHP Chinese website!