【发布时间】:2017-03-28 07:48:11
【问题描述】:
我正在尝试禁用从宏扩展的代码中的 g++ 警告。据我了解,_Pragma 应遵循宏用法,并且在使用 g++ 编译时不应触发 Wparentheses:
#include <stdio.h>
#define TEST(expr) \
int a = 1; \
_Pragma( "GCC diagnostic push" ) \
_Pragma( "GCC diagnostic ignored \"-Wparentheses\"" ) \
if (a <= expr) { \
printf("filler\n"); \
} \
_Pragma( "GCC diagnostic pop" )
int main(){
int b = 2, c = 3;
TEST(b == c);
}
当我用g++ 编译它时,我收到Wparentheses 警告,我正在尝试禁用它。
xarn@DESKTOP-B2A3CNC:/mnt/c/ubuntu$ g++ -Wall -Wextra test3.c
test3.c: In function ‘int main()’:
test3.c:8:11: warning: suggest parentheses around comparison in operand of ‘==’ [-Wparentheses]
if (a <= expr) { \
^
test3.c:15:5: note: in expansion of macro ‘TEST’
TEST(b == c);
^
但是在使用gcc 时它按预期工作:
xarn@DESKTOP-B2A3CNC:/mnt/c/ubuntu$ gcc -Wall -Wextra test3.c
test3.c: In function ‘main’:
test3.c:16:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
我使用的是g++ 4.8.5 版。
【问题讨论】:
-
gcc 4.8.5 不会编译带有
gcc -Wall -Wextra test3.c的发布源代码。由于iostream不是C 头文件,因此它应该以fatal error: iostream: No such file or directory失败。请贴出真实代码。 -
@MikeKinghan 抱歉,从引发问题的 .cpp 文件中复制粘贴。现在它使用
printf
标签: c++ c++11 gcc g++ c-preprocessor