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.
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.
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$
#error
can't be used inside a macro, though the question is vague as to the purpose.
– ideasman42
Aug 1 '14 at 10:03
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"
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)
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