MSVC defines _DEBUG
in debug mode, gcc defines NDEBUG
in release mode. What macro can I use in clang to detect whether the code is being compiled for release or debug?
问题:
回答1:
If you look at the project settings of your IDE, you will see that those macros are actually manually defined there, they are not automatically defined by the compiler. In fact, there is no way for the compiler to actually know if it's building a "debug" or "release", it just builds depending on the flags provided to it by the user (or IDE).
You have to make your own macros and define them manually, just like the IDE does for you when creating the projects.
回答2:
Compilers don't define those macros. Your IDE/Makefile/<insert build system here>
does. This doesn't depend on the compiler, but on the environment/build helper program you use.
The convention is to define the DEBUG
macro in debug mode and the NDEBUG
macro in release mode.
回答3:
You can use the __OPTIMIZE__
flag to determine if optimization is taking place. That generally means it is not a debug build since optimizations often rearrange the code sequence. Trying to step through optimized code can be confusing.
This probably is what those most interested in this question really are attempting to figure out.
回答4:
There is no such thing as a debug mode in a command line compiler. That is a IDE thing: it just sets up some options to be sent to the compiler.
If you use clang
from the command line, you can use whatever you want. The same is true for gcc
, so if with gcc
you use NDEBUG
you can use just the same.