如何抑制库头中的 GCC 警告?
我有一个使用 log4cxx、boost 等库的项目,其标头会生成大量(重复的)警告。有没有办法抑制来自库包含(即#include
I have a project that uses log4cxx, boost, etc. libraries whose headers generate lots of (repetitive) warnings. Is there a way to suppress warnings from library includes (i.e. #include <some-header.h>) or includes from certain paths? I'd like to use -Wall and/or -Wextra as usual on project code without relevant info being obscured. I currently use grep on make output but I'd like something better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以尝试使用
-isystem
而不是-I
来包含库标头。这将使它们成为“系统标头”,并且 GCC 不会为它们报告警告。You may try to include library headers using
-isystem
instead of-I
. This will make them "system headers" and GCC won't report warnings for them.对于使用 CMake 的用户,您可以修改
include_directories
指令以包含符号SYSTEM
,该符号会抑制针对此类标头的警告。For those using CMake, you can modify your
include_directories
directives to include the symbolSYSTEM
which suppresses warnings against such headers.您可以使用编译指示。例如:
You can use pragmas. For example:
我找到了窍门。对于库包含,请在 makefile 中使用
-isystem dir
,而不是-Idir
。然后,GCC 将 boost 等视为系统包含的内容,并忽略来自它们的任何警告。I found the trick. For library includes, instead of
-Idir
use-isystem dir
in the makefile. GCC then treats boost etc. as system includes and ignores any warnings from them.#pragma
是对编译器的指令。您可以在 #include 之前设置一些内容并在 #include 之后禁用它。您还可以在命令行中执行此操作。
另一个专门关于禁用警告的 GCC 页面。
我会选择在源代码中使用#pragma,然后提供
禁用警告的合理理由(作为评论)。这意味着要对头文件进行推理。
GCC 通过对警告类型进行分类来解决此问题。您可以将它们分类为警告或忽略。之前链接的文章将向您显示哪些警告可能被禁用。
注意:您还可以使用 属性;然而,这将您与 GCC 紧密地联系在一起。
注2:GCC 还使用 pop/push 接口,如微软的编译器——微软通过这个接口禁用警告。我建议你进一步调查这个问题,因为我不知道这是否可能。
#pragma
are instructions to the compiler. you can set something before the #include and disable it after.You can also do it at the command line.
Another GCC page specifically on disabling warnings.
I would go for the option of using #pragma's within the source code, and then providing a
sound reason (as a comment) of why you are disabling the warnings. This would mean reasoning about the headers files.
GCC approaches this by classifying the warning types. You can classify them to be warnings or to be ignored. The previously linked articles will show you which warnings are may be disabled.
Note: you can also massage the source code to prevent certain warnings by using attributes; however, this bind you quite closely to GCC.
Note2: GCC also uses the pop/push interface as used in microsoft's compiler -- Microsoft disables warnings through this interface. I suggest you investigate this further , as I do not know if it is even possible.
放置以下内容
将关闭此文件中所有以下代码的 GCC 警告。
Putting the following
will turn off GCC warnings for all following code in this file.
您可以尝试使用预编译标头。警告不会消失,但至少不会出现在您的主编译中。
You can try using precompiled headers. Warnings won't go away but at least the won't show up in your main compilation.
如果您需要显式覆盖系统标头,那么您只能使用编译指示。您可以通过
make dependent
输出来验证您正在使用哪些内容。另请参阅 gcc 的诊断推送-弹出 >= 4.6
If you need to explicitly override a system header then you're restricted to pragmas. You can verify which includes you're using via
make depend
output.Also see diagnostic push-pop for gcc >= 4.6
另一种方法是在 makefile 中告诉编译器忽略特定文件夹的警告:
Another way to do it is, in the makefile, to tell the compiler to ignore warnings for the specific folder:
这些警告一定是有原因的。这些可能是由使用该库的代码中的错误引起的,或者是由库代码本身的错误引起的。在第一种情况下,修复您的代码。在第二种情况下,要么停止使用该库,要么如果它是 FOSS 代码,请修复它。
There must be reasons for those warnings. These will either be caused by errors in your code that uses the library, or by errors in the library code itself. In the first case, fix your code. In the second case, either stop using the library or if it is FOSS code, fix it.