只对部分翻译单元select性地禁用GCC警告?

与这个MSVC预处理器代码最接近的GCC是什么?

#pragma warning( push ) // Save the current warning state. #pragma warning( disable : 4723 ) // C4723: potential divide by 0 // Code which would generate warning 4723. #pragma warning( pop ) // Restore warnings to previous state. 

我们在通常包含的头文件中有代码,我们不希望产生特定的警告。 不过,我们希望包含这些头文件的文件继续生成该警告(如果项目启用了该警告)。

从GCC 4.6版本开始,在2010年6月左右的时候,这在GCC中是可行的。

这是一个例子:

 #pragma GCC diagnostic error "-Wuninitialized" foo(a); /* error is given for this one */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wuninitialized" foo(b); /* no diagnostic for this one */ #pragma GCC diagnostic pop foo(c); /* error is given for this one */ #pragma GCC diagnostic pop foo(d); /* depends on command line options */ 

最接近的是GCC诊断编译指示 , #pragma GCC diagnostic [warning|error|ignored] "-Wwhatever" 。 这是不是很接近你想要的,看到链接的细节和注意事项。

我做了类似的事情。 对于第三方代码,我不想看到任何警告。 因此, -I/path/to/libfoo/include没有指定-I/path/to/libfoo/include ,而是使用了-isystem /path/to/libfoo/include 。 这使编译器将这些头文件当作“系统头文件”来处理警告,只要你不启用-Wsystem-headers ,那么你大都是安全的。 我仍然看到一些警告泄漏出去,但是它减less了大部分的垃圾。

请注意,这只能帮助你,如果你可以通过include-directory来隔离有问题的代码。 如果它只是你自己项目的一个子集,或者与其他代码混合在一起,那么你倒霉了。