2017-10-27 6 views
1

Ich habe ein Code-Snippet, hier habe ich eine #pragma setzen. Dies gibt Wunknown-pragmas Warnung:Wie zu beheben Wunknown-Pragmas gcc Warnung

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

Code:

#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; 
} 

Wie kann ich dieses Problem beheben auf Code-Ebene?

+1

AFAIK, Pragmas sind compilerspezifisch. Sind Sie sicher, dass es für Ihren Fall anwendbar ist? –

+2

Ich bin etwas verwirrt über die '#ifdef __GNUC__'. Wie der Compiler sagt - es kann das '# Pragma' nicht lesen und ignoriert es daher. Dass es eine Warnung gibt, wird mit der Befehlszeilenoption '-Wunknown-pragmas' aktiviert. Übrigens. es sieht wie ein [MS VC-Pragma] (https://msdn.microsoft.com/en-us/de-de/library/2c8f766e.aspx) im Gegensatz zum [GCC-Pragma] aus (https: //gcc.gnu) .org/onlinedocs/gcc/Diagnose-Pragmas.html). – Scheff

Antwort

0

So verwenden Sie nicht Pragmas in GCC. Es sollte eher sein:

#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; 
} 

die gcc manual für weitere Informationen.