开发者

GCC #pragma to stop compilation

开发者 https://www.devze.com 2022-12-17 18:05 出处:网络
Is there a GCC pragma directive that will stop, halt, or abort the compilation process? I am using 开发者_如何学JAVAGCC 4.1, but I would want the pragma to be available in GCC 3.x versions also.You pr

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

I am using 开发者_如何学JAVAGCC 4.1, but I would want the pragma to be available in GCC 3.x versions also.


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;
}


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


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


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)


#pragma GCC error "error message"

Ref: 7 Pragmas


You can use:

#pragma GCC error "my message"

But it is not standard.

0

精彩评论

暂无评论...
验证码 换一张
取 消