Recently, I needed to control the logic direction through a C code in my work. I searched online and found that I could dynamically define the macro switch in the CMakeLists file, so as to achieve the compilation of different logic flow codes.
Specific steps:
First, I wrote some debug output in the SRC code:
#IFDEF DEBUG
some print command;
#ENDIF
Copy the code
Then, add the definition of DEBUG to the CMakeLists file:
IF (CMAKE_BUILD_TYPE STREQUAL DEBUG)
ADD_DEFINITIONS(-DDEBUG)
ENDIF()
Copy the code
Finally, set the parameter -dcmake_build_type to DEBUG on cmake:
$ cmake .. -DCMAKE_BUILD_TYPE=DEBUG
$ make -j4
Copy the code
This will print some print command debug messages when running the executable again. If you do not want to see debugging information, do not set the debug parameter in parameters or set the debug parameter to another value. You can do either of the following:
$ cmake ..
$ cmake .. -DCMAKE_BUILD_TYPE=RELEASE
Copy the code
To this CMakeLists implementation dynamic macro switch introduction is complete.