What predefined macro can be used to detect debug build with clang?

前端 未结 4 1087
梦毁少年i
梦毁少年i 2021-02-20 03:20

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 r

相关标签:
4条回答
  • 2021-02-20 03:46

    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.

    0 讨论(0)
  • 2021-02-20 03:51

    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.

    0 讨论(0)
  • 2021-02-20 03:56

    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.

    0 讨论(0)
  • 2021-02-20 04:04

    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.

    0 讨论(0)
提交回复
热议问题