patterncppCritical
Define a preprocessor macro through CMake
Viewed 0 times
cmakepreprocessormacrodefinethrough
Problem
How do I define a preprocessor variable through CMake?
The equivalent code would be
The equivalent code would be
#define foo.Solution
For a long time, CMake only had the
An example using the new add_compile_definitions:
Or:
The good part about this is that it circumvents the shabby trickery CMake has in place for
Find more explanation on which commands to use for compiler flags here: https://cmake.org/cmake/help/latest/command/add_definitions.html
Likewise, you can do this per-target as explained in Jim Hunziker's answer.
add_definitions command for this purpose. However, since CMake version 3.12 (released July 2018) the command has been superseded by a more fine grained approach (separate commands for compile definitions, include directories, and compiler options).An example using the new add_compile_definitions:
add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION})
add_compile_definitions(WITH_OPENCV2)Or:
add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION} WITH_OPENCV2)The good part about this is that it circumvents the shabby trickery CMake has in place for
add_definitions. CMake is such a shabby system, but they are finally finding some sanity.Find more explanation on which commands to use for compiler flags here: https://cmake.org/cmake/help/latest/command/add_definitions.html
Likewise, you can do this per-target as explained in Jim Hunziker's answer.
Code Snippets
add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION})
add_compile_definitions(WITH_OPENCV2)add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION} WITH_OPENCV2)Context
Stack Overflow Q#9017573, score: 558
Revisions (0)
No revisions yet.