ffmpeg编译问题

利用ffmpeg实现一个播放器,ffmpeg提供动态库,但是编译链接的时候遇到下面的问题:

../ffmpegWidgetPlayer/videoplayerwidget.cpp:23: error: undefined reference to 'sws_freeContext(SwsContext*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:28: error: undefined reference to 'av_frame_free(AVFrame**)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:33: error: undefined reference to 'av_frame_free(AVFrame**)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:38: error: undefined reference to 'avcodec_close(AVCodecContext*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:39: error: undefined reference to 'avcodec_free_context(AVCodecContext**)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:44: error: undefined reference to 'avformat_close_input(AVFormatContext**)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:55: error: undefined reference to 'avformat_network_init()'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:58: error: undefined reference to 'avformat_open_input(AVFormatContext**, char const*, AVInputFormat const*, AVDictionary**)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:64: error: undefined reference to 'avformat_find_stream_info(AVFormatContext*, AVDictionary**)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:84: error: undefined reference to 'avcodec_find_decoder(AVCodecID)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:91: error: undefined reference to 'avcodec_alloc_context3(AVCodec const*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:98: error: undefined reference to 'avcodec_parameters_to_context(AVCodecContext*, AVCodecParameters const*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:104: error: undefined reference to 'avcodec_open2(AVCodecContext*, AVCodec const*, AVDictionary**)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:110: error: undefined reference to 'av_frame_alloc()'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:117: error: undefined reference to 'av_frame_alloc()'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:124: error: undefined reference to 'av_image_get_buffer_size(AVPixelFormat, int, int, int)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:125: error: undefined reference to 'av_malloc(unsigned long)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:128: error: undefined reference to 'av_image_fill_arrays(unsigned char**, int*, unsigned char const*, AVPixelFormat, int, int, int)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:131: error: undefined reference to 'sws_getContext(int, int, AVPixelFormat, int, int, AVPixelFormat, int, SwsFilter*, SwsFilter*, double const*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:148: error: undefined reference to 'av_read_frame(AVFormatContext*, AVPacket*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:151: error: undefined reference to 'avcodec_send_packet(AVCodecContext*, AVPacket const*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:157: error: undefined reference to 'avcodec_receive_frame(AVCodecContext*, AVFrame*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:164: error: undefined reference to 'sws_scale(SwsContext*, unsigned char const* const*, int const*, int, int, unsigned char* const*, int const*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:170: error: undefined reference to 'av_frame_unref(AVFrame*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:174: error: undefined reference to 'av_packet_unref(AVPacket*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:187: error: undefined reference to 'sws_freeContext(SwsContext*)'
../ffmpegWidgetPlayer/videoplayerwidget.cpp:188: error: undefined reference to 'sws_getContext(int, int, AVPixelFormat, int, int, AVPixelFormat, int, SwsFilter*, SwsFilter*, double const*)'
collect2: error: ld returned 1 exit status
make: *** [Makefile:288: ffmpegWidgetPlayer] Error 1
10:29:50: 进程"/usr/bin/make"退出,退出代码 2 。
Error while building/deploying project ffmpegWidgetPlayer (kit: Desktop Qt 5.15.2 GCC 64bit)
When executing step "Make"
10:29:50: Elapsed time: 00:03.

 通过分析报错信息可以看到在链接阶段提示ffmpeg相关的api找不到符号,看到这个问题百思不得其解,该工程我是利用qt编译链接,可以肯定的是pro文件中已经通过LIBS关键字指明了依赖库的路径和依赖库的名称,但是仍然提示找不到,最后通过查阅资料,才了解问题产生的原因:qt工程是c++代码,ffmpeg是c原因编写的,当c++代码调用C库时对c++代码中调用的头文件需要使用extern "C"包含起来,以此让编译器按照C的方式编译ffmpeg相关的头文件

解决方法如下:

#include 
#include 
#include 
#include 
#include 
#include 
extern "C"   //按照C语言方式编译api接口
{
#include 
#include 
#include 
#include 
#include 
#include 
#include 
}

总结

下面对编译阶段“undefined reference to”的报错原因进行总结,希望能够大家有所帮助:

  • 产生这个报错简单的是就是依赖库未包含,就是说未指定依赖库的路径和和依赖库名称,这个通过LIBS关键字就可以解决
  • 如果确定LIBS等类似的方式已经指明依赖库的路径和依赖库名称,还是有这个报错,那么确认下是否是C++代码调用了C语言的库,如果属实,那么就是用extern "C"关键字包含C库的头文件

你可能感兴趣的:(ffmpeg,c++,ffmpeg,c++,c)