【问题标题】:How to fix Wunknown-pragmas gcc warning如何修复 Wunknown-pragmas gcc 警告
【发布时间】:2017-10-27 09:35:54
【问题描述】:

我有一个代码 sn-p,这里我放了一个#pragma。这给出了Wunknown-pragmas 警告:

warning: ignoring #pragma warning  [-Wunknown-pragmas]

代码:

#include<iostream>

using namespace std;

int main(){
  cout<<"Helloworld\n";

  #ifdef __GNUC__
  #pragma warning( push )
  #pragma warning( disable : warning )
  cout<<  "I am in warning free section"<<endl;
  #pragma warning( pop )

  #endif

  return 0;
} 

如何在代码级别解决此问题?

【问题讨论】:

  • AFAIK,编译指示是编译器特定的。您确定它适用于您的情况吗?
  • 我对@9​​87654327@ 有点疑惑。正如编译器所说 - 它无法读取 #pragma 并因此忽略它。使用命令行选项-Wunknown-pragmas 启用它会产生警告。顺便提一句。它看起来像 MS VC pragmaGCC pragma 相对。

标签: c++ gcc pragma gcc-warning


【解决方案1】:

这不是您在 GCC 中使用编译指示的方式。它应该更像:

#include<iostream>

 //example function that
 //complains if its result is unused
__attribute__((__warn_unused_result__)) int foo() { return 42; }

using namespace std;
int main(){
  cout<<"Helloworld\n";
  foo();

  #ifdef __GNUC__
  #pragma GCC diagnostic push
  #pragma GCC diagnostic ignored "-Wunused-result" 
  foo(); //no complaints here
  cout<<  "I am in warning free section"<< endl;
  #pragma GCC diagnostic pop

  #endif

   return 0;
}

请参阅gcc manual 了解更多信息。

【讨论】:

    猜你喜欢
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 2020-11-28
    • 2017-10-25
    • 2017-07-03
    • 1970-01-01
    相关资源
    最近更新 更多