GCC #pragma to stop compilation

0 votes
asked Jan 23, 2010 by sean-a-o-harney

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

I am using gcc 4.1 but would want the pragma to be available on gcc 3.x versions also.

6 Answers

0 votes
answered Jan 23, 2010 by michael

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

#error Failing compilation

Will terminate compilation with the error message "Failing compilation"

0 votes
answered Jan 23, 2010 by dirk-eddelbuettel

You probably want #error:

edd@ron:/tmp$ g++ -Wall -DGoOn -o stopthis stopthis.cpp
edd@ron:/tmp$ ./stopthis
Hello, world
edd@ron:/tmp$ g++ -Wall -o stopthis stopthis.cpp
stopthis.cpp:7:6: error: #error I had enough
edd@ron:/tmp$ cat stopthis.cpp

#include <iostream>

int main(void) {
  std::cout << "Hello, world\n";
  #ifndef GoOn
    #error I had enough
  #endif
  return 0;
}
edd@ron:/tmp$
0 votes
answered Jan 1, 2014 by ideasman42

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)
0 votes
answered Jan 10, 2015 by leo
#pragma GCC error "error message"

https://gcc.gnu.org/onlinedocs/cpp/Pragmas.html

0 votes
answered Sep 15, 2017 by klevh

You can use :

#pragma GCC error "my message"

But it is not standard.

0 votes
answered Sep 15, 2017 by mosh

This works:

 #include <stophere>

gcc stops when it can't find the include file. I wanted gcc to stop if C14 was not supported.

 #if __cplusplus<201300L
  #error need g++14
  #include <stophere>
#endif 
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...