'How to add custom flag in CMake building
My main goal is to use a flag when building a project in command line. So I can add own flags like DEBUG or MY_FLAG.
#ifdef DEBUG
#define debug_print(format, ...) printf(format, __VA_ARGS__)
#else
#define debug_print(...)
#endif
int main() {
debug_print("%s\n", "Debugging mode");
return 0;
}
I build the program with:
cmake -H. -Bbuild
make -C build -j
then run with
./build/main
I added add_definitions(-DDEBUG)
to the CMakeList file which works fine.
But I want to add the flag in the cmake -H. -Build
process.
I tried with cmake -H. -Build -DCMAKE_BUILD_TYPE=DEBUG
but it's not working.
Solution 1:[1]
I had a similar problem. The way I solved it was by adding this line to CMakeLists.txt:
add_compile_options("$<$<NOT:$<CONFIG:RELEASE,MINSIZEREL,RELWITHDEBINFO>>:-DDEBUG>")
This way, whenever -DMAKE_BUILD_TYPE
is set to either RELEASE
, MINSIZERE
, or RELWITHDEBINFO
, my custom flag DEBUG
will not be defined.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | chiasmos |