41

Is there a GCC pragma directive that will stop, halt, or abort the compilation process?

I am using GCC 4.1, but I would want the pragma to be available in GCC 3.x versions also.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean A.O. Harney
  • 23,901
  • 4
  • 30
  • 30

7 Answers7

61

You probably want #error:

$ cd /tmp
$ g++ -Wall -DGoOn -o stopthis stopthis.cpp
$ ./stopthis

Hello, world

$ g++ -Wall -o stopthis stopthis.cpp

stopthis.cpp:7:6: error: #error I had enough

File stopthis.cpp

#include <iostream>

int main(void) {
  std::cout << "Hello, world\n";
  #ifndef GoOn
    #error I had enough
  #endif
  return 0;
}
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • A limitation here is `#error` can't be used inside a macro, though the question is vague as to the purpose. – ideasman42 Aug 01 '14 at 10:03
  • 6
    I thought this too, however my GCC (4.9) isn't stopping with an error, it carries on, obviously it doesn't compile but it doesn't halt, is this a bug or can you confirm? – Alec Teal Jan 15 '16 at 23:53
21

I do not know about a #pragma, but #error should do what you want:

#error Failing compilation

It will terminate compilation with the error message "Failing compilation".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael
  • 54,279
  • 5
  • 125
  • 144
13

This works:

 #include <stophere>

GCC stops when it can't find the include file. I wanted GCC to stop if C++14 was not supported.

 #if __cplusplus<201300L
   #error need g++14
   #include <stophere>
#endif
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mosh
  • 1,402
  • 15
  • 16
  • This is the best answer. It prevents the compiler from continuing and potentially spamming lots of unhelpful errors that occur after the #error. Those later errors may be misleading to some since they are likely just side effects of the first error. – phatpaul Feb 24 '23 at 14:42
6

While typically #error is sufficient (and portable), there are times when you want to use a pragma, namely, when you want to optionally cause an error within a macro.

Here is an example use which depends on C11's _Generic and _Pragma.

This example ensures var isn't an int * or a short *, but not a const int * at compile time.

Example:

#define MACRO(var)  do {  \
    (void)_Generic(var,   \
          int       *: 0, \
          short     *: 0, \
          const int *: 0 _Pragma("GCC error \"const not allowed\""));  \
    \
    MACRO_BODY(var); \
} while (0)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ideasman42
  • 42,413
  • 44
  • 197
  • 320
3
#pragma GCC error "error message"

Ref: 7 Pragmas

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Leo
  • 790
  • 8
  • 10
0

You can use:

#pragma GCC error "my message"

But it is not standard.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Klevh
  • 11
  • 5
0

An alternative is to use static_assert:

#if defined(_MSC_VER) && _MSC_VER < 1916
    static_assert(false, "MSVC supported versions are 1916 and later");
#endif
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93