43

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.

2
  • 1
    We might be able to provide a better answer if you tell us why you want compilation to stop.
    – Michael
    Commented Jan 23, 2010 at 22:22
  • Is the constraint on GCC 3-4.1 still relevant?
    – ideasman42
    Commented Dec 31, 2016 at 2:23

7 Answers 7

63

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;
}
2
  • A limitation here is #error can't be used inside a macro, though the question is vague as to the purpose.
    – ideasman42
    Commented Aug 1, 2014 at 10:03
  • 7
    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
    Commented Jan 15, 2016 at 23:53
23

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".

0
14

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
2
  • 1
    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
    Commented Feb 24, 2023 at 14:42
  • #error would halt compilation. Why did you include #include line after that? Commented Jul 9 at 2:00
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)
4
#pragma GCC error "error message"

Ref: 7 Pragmas

0

You can use:

#pragma GCC error "my message"

But it is not standard.

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
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.