How do I define a preprocessor variable through CMake?
The equivalent code would be #define foo.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For a long time, CMake had the
add_definitionscommand for this purpose. However, recently 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:
Or:
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.