77

I have a program that must be compiled only in DEBUG mode. (testing purpose)

How can I have the preprocessor prevent compilation in RELEASE mode?

phs
  • 10,687
  • 4
  • 58
  • 84
eonil
  • 83,476
  • 81
  • 317
  • 516
  • Possible duplicate of [Create custom #warning flags](https://stackoverflow.com/questions/4168245/create-custom-warning-flags) – Jim Fell May 15 '18 at 17:23

7 Answers7

102

Place anywhere:

#ifndef DEBUG
#error "Only Debug builds are supported"
#endif

For reference: Diagnostics

ejohnso49
  • 1,336
  • 1
  • 15
  • 20
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
27

C provide a #error statement, and most compilers add a #warning statement. The gcc documentation recommends to quote the message.

philant
  • 34,748
  • 11
  • 69
  • 112
  • 1
    @Antonio Right, there is no [more] recommendation there. I replaced the link with one to gcc doc. – philant Nov 17 '15 at 17:29
21

Maybe something more sofisticated, but it is only copy&paste of previous solutions. :-)

#ifdef DEBUG        
    #pragma message ( "Debug configuration - OK" )
#elif RELEASE   
    #error "Release configuration - WRONG"
#else
    #error "Unknown configuration - DEFINITELY WRONG"
#endif

P.S. There is also another way how to generate a warning. Create an unreferenced label like

HereIsMyWarning:

and don't reference it. During compilation, you will get a warning like

 1>..\Example.c(71) : warning C4102: 'HereIsMyWarning' : unreferenced label
Paul Wintz
  • 2,542
  • 1
  • 19
  • 33
Zdeno Pavlik
  • 728
  • 6
  • 15
  • 3
    `#pragma message ( "Debug configuration - OK" )` works (only) for MSVC, while `#warning "..."` doesn't work for MSVC, but works for gcc and clang. – ead Nov 19 '20 at 22:03
5

You can use a error directive for that. The following code will throw an error at compile time if DEBUG is not defined:

#ifndef DEBUG
#error This is an error message
#endif
Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
3

If you simply want to report an error:

#ifdef RELEASE
  #error Release mode not allowed
#endif

will work with most compilers.

3

For GCC and Clang (and probably any compiler that supports the _Pragma feature) you can define a macro:

#if ! DEBUG
#define FIX_FOR_RELEASE(statement) _Pragma ("GCC error \"Must be fixed for release version\"")
#else
#define FIX_FOR_RELEASE(statement) statement
#endif

You can use this macro for temporary hacks, for example to get around code that a co-worker hasn't written yet, to make sure you don't forget to fix it once you want to release a build to the public. Either

FIX_FOR_RELEASE()
// Code that must be removed or fixed before you can release

or

FIX_FOR_RELEASE(statement that must be removed or fixed before you can release);
gnasher729
  • 51,477
  • 5
  • 75
  • 98
1

In Code::Blocks, if you don't want the Release mode, you can delete the Release mode. To do this, click on the Project menu, select Properties..., and in the Build targets tab you can click on Release and then click on the Delete button. Deleting the Release mode only does it for the current project, so you can still use it in other projects.

Otherwise, if you really want to use the preprocessor, you can do this:

#ifdef RELEASE
#error "You have to use the Debug mode"
#endif
Donald Duck
  • 8,409
  • 22
  • 75
  • 99