【问题标题】:gcc 4.8.4 "pragma GCC diagnostic not applied"gcc 4.8.4“未应用编译指示 GCC 诊断”
【发布时间】:2015-08-13 23:27:25
【问题描述】:

使用 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

【问题讨论】:

  • 如果关闭-Werror会怎样?
  • 这不是与该错误线程上链接的其他 SO 问题的重复吗?
  • @LightnessRacesinOrbit 请注意,我的问题是,“我想知道是否有人有这个工作?”。他也在询问 4.7,而我询问的是 4.8.4。这个错误可能已经解决了。请注意,该错误指出它在 4.8.0 中。所以理论上它是一个跟进,看看是否有人在 4.8.4 中解决了这个问题。
  • @LightnessRacesinOrbit 关闭 -Werror 不处理除我指定的警告之外的警告。另外,根据 gcc 的文档,“请注意,这些 pragma 会覆盖任何命令行选项。”见gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
  • 我知道-Werror 做了什么。我在问它对这个错误有什么影响。

标签: c++ gcc c-preprocessor pragma


【解决方案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;
}
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    相关资源
    最近更新 更多