2010-01-23 68 views
25

是否有GCC编译指示会停止,暂停或中止编译过程?GCC#pragma停止编译

我正在使用gcc 4.1,但希望该编译指示可以在gcc 3.x版本上使用。

+1

如果您告诉我们为什么要编译停止,我们可能会提供更好的答案。 – Michael 2010-01-23 22:22:29

+0

对GCC 3-4.1的约束是否仍然相关? – ideasman42 2016-12-31 02:23:02

回答

38

你可能想#error

[email protected]:/tmp$ g++ -Wall -DGoOn -o stopthis stopthis.cpp 
[email protected]:/tmp$ ./stopthis 
Hello, world 
[email protected]:/tmp$ g++ -Wall -o stopthis stopthis.cpp 
stopthis.cpp:7:6: error: #error I had enough 
[email protected]:/tmp$ cat stopthis.cpp 

#include <iostream> 

int main(void) { 
    std::cout << "Hello, world\n"; 
    #ifndef GoOn 
    #error I had enough 
    #endif 
    return 0; 
} 
[email protected]:/tmp$ 
+0

这里的一个限制是'#error'不能在宏内部使用,尽管这个问题在目的上是模糊的。 – ideasman42 2014-08-01 10:03:11

+3

我也这么认为,但是我的GCC(4.9)没有停止出错,它继续,显然它不能编译,但它不会停止,这是一个错误还是可以确认? – 2016-01-15 23:53:45

15

我不知道一个#pragma,但#error应该做你想要什么:

#error Failing compilation 

将终止与错误信息汇编“失败汇编”

6

虽然通常#error是足够的(和便携式),有时间当你想要使用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) 
1

这种工作原理:当它不能找到包含文件

#include <stophere> 

GCC停止。如果C14不受支持,我希望gcc停止。

#if __cplusplus<201300L 
    #error need g++14 
    #include <stophere> 
#endif 
0

您可以使用:

#pragma GCC error "my message" 

但它不是标准。