使用g ++(Ubuntu 4.8.4-2ubuntu1~14.04)4.8.4
在预编译的标题中,我有以下内容:
63 #pragma GCC diagnostic push
64 #pragma GCC diagnostic ignored "-Wunused-variable"
65 #include <boost/filesystem.hpp>
66 #pragma GCC diagnostic pop
现在,当我运行构建系统时,出现以下构建错误:
from <>../../../../Core_Pch.h:65,
from <command-line>:0:
<>/../../../../external/include/BoostBase/boost/system/error_code.hpp: At global scope:
<>/../../../../external/include/BoostBase/boost/system/error_code.hpp:221:36: error: ‘boost::system::posix_category’ defined but not used [-Werror=unused-variable]
static const error_category & posix_category = generic_category();
GCC bug tracker似乎已经打开了一个可能的错误。但是,我想知道是否有人这样工作?该漏洞证实了 C 词法分析器使用的预处理器的行为与C ++词法分析器使用的行为不同。
这可能与我们的构建系统中的其他内容有关。请注意,如果我创建最简单的示例:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
int main(void)
{
int x;
return 0;
}
#pragma GCC diagnostic pop
如果我调用它,这将按预期工作:
~/Devel/pragma $ gcc -Werror -Wall -pedantic main.c
~/Devel/pragma $ g++ -Werror -Wall -pedantic main.c
注释掉忽略的行会导致:
mhoggan@mhoggan-Precision-T3600 ~/Devel/pragma $ gcc -Werror -Wall -pedantic main.c
main.c:2:1: error: C++ style comments are not allowed in ISO C90 [-Werror]
//#pragma GCC diagnostic ignored "-Wunused-variable"
^
main.c:2:1: error: (this will be reported only once per input file) [-Werror]
main.c: In function ‘main’:
main.c:5:7: error: unused variable ‘x’ [-Werror=unused-variable]
int x;
^
cc1: all warnings being treated as errors
mhoggan@mhoggan-Precision-T3600 ~/Devel/pragma $ g++ -Werror -Wall -pedantic main.c
main.c: In function ‘int main()’:
main.c:5:7: error: unused variable ‘x’ [-Werror=unused-variable]
int x;
^
cc1plus: all warnings being treated as errors
答案 0 :(得分:1)
我在g ++ 5.3.0上也收到了同样的警告。通过在以下定义下添加虚拟函数,我发现了一个讨厌的解决方案:
...
static const error_category & posix_category = generic_category();
static const error_category & errno_ecat = generic_caregory();
static const error_category & native_ecat = system_category();
inline void dummy()
{
(void) posix_category;
(void) errno_ecat;
(void) native_ecat;
}
...