如何在外部库头文件中忽略GCC编译器的“pedantic”错误?

16
我最近在我的GCC编译选项中添加了-pedantic-pedantic-errors,以帮助清理我的跨平台代码。一切都很好,直到它在外部包含的头文件中发现错误。有没有办法关闭这些外部头文件中的错误检查,例如:

继续检查像这样包含的文件:

#include "myheader.h"

停止像这样检查包含文件:

#include <externalheader.h>

这里是我遇到的错误:

g++ -Wall -Wextra -Wno-long-long -Wno-unused-parameter -pedantic --pedantic-errors
-O3 -D_FILE_OFFSET_BITS=64 -DMINGW -I"freetype/include" -I"jpeg" -I"lpng128" -I"zlib"
-I"mysql/include" -I"ffmpeg/libswscale" -I"ffmpeg/libavformat" -I"ffmpeg/libavcodec"
-I"ffmpeg/libavutil" -o omingwd/kguimovie.o -c kguimovie.cpp

In file included from ffmpeg/libavutil/avutil.h:41,
             from ffmpeg/libavcodec/avcodec.h:30,
             from kguimovie.cpp:44:
ffmpeg/libavutil/mathematics.h:32: error: comma at end of enumerator list
In file included from ffmpeg/libavcodec/avcodec.h:30,
             from kguimovie.cpp:44:
ffmpeg/libavutil/avutil.h:110: error: comma at end of enumerator list
In file included from kguimovie.cpp:44:
ffmpeg/libavcodec/avcodec.h:277: error: comma at end of enumerator list
ffmpeg/libavcodec/avcodec.h:303: error: comma at end of enumerator list
ffmpeg/libavcodec/avcodec.h:334: error: comma at end of enumerator list
ffmpeg/libavcodec/avcodec.h:345: error: comma at end of enumerator list
ffmpeg/libavcodec/avcodec.h:2249: warning: `ImgReSampleContext' is deprecated
(declared at ffmpeg/libavcodec/avcodec.h:2243)
ffmpeg/libavcodec/avcodec.h:2259: warning: `ImgReSampleContext' is deprecated
(declared at ffmpeg/libavcodec/avcodec.h:2243)
In file included from kguimovie.cpp:45:
ffmpeg/libavformat/avformat.h:262: error: comma at end of enumerator list
In file included from ffmpeg/libavformat/rtsp.h:26,
             from ffmpeg/libavformat/avformat.h:465,
             from kguimovie.cpp:45:
ffmpeg/libavformat/rtspcodes.h:38: error: comma at end of enumerator list
In file included from ffmpeg/libavformat/avformat.h:465,
             from kguimovie.cpp:45:
ffmpeg/libavformat/rtsp.h:32: error: comma at end of enumerator list
ffmpeg/libavformat/rtsp.h:69: error: comma at end of enumerator list
5个回答

34
使用 -Wsystem-headers 选项,GCC 将打印与系统头文件相关的警告消息,这些消息通常会被抑制。 但是,您希望 GCC 基本上将这些文件视为系统头文件,因此您可以尝试传递“-isystem /usr/local/ffmpeg”(或者您安装该软件包的任何位置),以使 GCC 忽略来自这些目录中包含的文件的错误。

1

我不知道有没有办法告诉GCC停止发出那些警告。但是,你可以使用类似于llvm-gcc(或者只是gcc)-pedantic 2>&1|grep -v "/usr/"这样的方法来删除第三方警告。


0

我脑海中浮现出一个想法(我不知道是否有“开箱即用”的参数):

准备一个脚本,它将获取您编译器的输出,并删除所有包含未在特定列表中的头文件(您的头文件)的行。

这种方法应该不难实现。


-2

目前你不能告诉GCC只对某些头文件进行严格检查而不是其他头文件。你可以建议将其作为一个功能,但我怀疑它会被拒绝,因为理想情况下每个人都应该严格要求。

你可以自己修复头文件,生成补丁,然后在升级库的时候将该补丁应用于后续版本的头文件。

同时将补丁提交给FFmpeg,希望他们能够采纳,但无论如何,即使他们不接受,你也有所准备。


ffmpeg 头文件使用 -pedantic -std=c99 是有效的,这也是它用于构建的。但我猜公共头文件无论如何应该也是有效的,不管是否使用了 whatever-extern-"C"。 - alex strange

-4

你可以修复头文件并向FFmpeg提交补丁;与-pedantic的兼容性是一个值得追求的目标,所以我相信他们会考虑的,特别是如果只涉及到删除尾随逗号等问题。


11
这并没有真正回答这个问题。 - Nick
1
@KPexEA:请考虑重新分配采纳答案给明显更好的答案——这有助于SO搜索结果和人类读者找到答案。 - sehe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接