GCC#pragma停止编译

Sea*_*ney 32 gcc

是否存在将停止,暂停或中止编译过程的GCC编译指示?

我正在使用gcc 4.1,但希望pragma也可以在gcc 3.x版本上使用.

Dir*_*tel 48

你可能想要#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$
Run Code Online (Sandbox Code Playgroud)

  • 我也这么认为,但是我的GCC(4.9)并没有因为错误而停止,它继续,显然它没有编译但它没有停止,这是一个错误还是你能确认吗? (4认同)

Mic*_*ael 16

我不知道a #pragma,但#error应该做你想做的事:

#error Failing compilation
Run Code Online (Sandbox Code Playgroud)

将使用错误消息"Failing compilation"终止编译


ide*_*n42 6

虽然通常#error足够(并且可移植),但有时您想要使用a pragma,即,当您想要在宏中导致错误时.

这是一个示例用途,它取决于C11 _Generic_Pragma

此示例确保在编译时var不是int *short *不是const int *.

例:

#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)
Run Code Online (Sandbox Code Playgroud)


mos*_*osh 6

这有效:

 #include <stophere>
Run Code Online (Sandbox Code Playgroud)

gcc在找不到包含文件时停止.如果不支持C14,我希望gcc停止.

 #if __cplusplus<201300L
  #error need g++14
  #include <stophere>
#endif 
Run Code Online (Sandbox Code Playgroud)