【FFmpeg】解决警告warning: xxx is deprecated [-Wdeprecated-declarations]的方法

1、问题描述

编译FFmpeg程序时,经常报一些关于“deprecated”的警告信息,具体内容如下:

decode.cpp:28:2: warning:void av_register_all()’ is deprecated [-Wdeprecated-declarations]
  av_register_all();
decode.cpp:34:2: warning:void avcodec_register_all()’ is deprecated [-Wdeprecated-declarations]
  avcodec_register_all();
2、原因分析

根据信息提示,这些接口已经过时,在编写程序时推荐使用。查看FFmpeg源码,对不推荐使用的接口、变量、类等都被宏attribute_deprecated标记,源码如下:

// avformat.h
attribute_deprecated
void av_register_all(void);

// avcodec.h
attribute_deprecated
void avcodec_register_all(void);

该宏定义在libavutil/attributes.h

 93 #if AV_GCC_VERSION_AT_LEAST(3,1)
 94 #    define attribute_deprecated __attribute__((deprecated))
 95 #elif defined(_MSC_VER)
 96 #    define attribute_deprecated __declspec(deprecated)
 97 #else
 98 #    define attribute_deprecated
 99 #endif

gcc和VC编译器分别使用__attribute__((deprecated))和__declspec(deprecated)来管理过时的代码。

3、解决方法

以avcodec_register(), avcodec_register_all()为例,先查询这些接口在哪个版本中过时的,参考博客【FFmpeg】截至ffmpeg4.2不推荐(Deprecate)继续使用的接口,以及代替它的接口汇总

2018-02-06 - 0694d87024 - lavf 58.9.100 - avformat.h
Deprecate use of av_register_input_format(), av_register_output_format(), av_register_all(), av_iformat_next(), av_oformat_next().

2018-02-06 - 36c85d6e77 - lavc 58.10.100 - avcodec.h
Deprecate use of avcodec_register(), avcodec_register_all(), av_codec_next(), av_register_codec_parser(), and av_parser_next().

可以直接删除或替换这些过时的接口,也可以使用宏做处理,这样可以兼容新旧版本库。

#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  av_register_all();
#endif
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
  avcodec_register_all();
#endif
发布了324 篇原创文章 · 获赞 266 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/u010168781/article/details/105163945