【ffmpeg】编译时报错:error: undefined reference to `av...

1、问题描述

昨天使用ffmpeg库编译demo一切正常,今天再次链接ffmpeg库时报了一堆错误:

error: undefined reference to `av_frame_alloc()'
error: undefined reference to `avio_close(AVIOContext*)'
error: undefined reference to `avcodec_find_encoder(AVCodecID)'
error: undefined reference to `avcodec_alloc_context3(AVCodec const*)'
error: undefined reference to `av_opt_set(void*, char const*, char const*, int)'
error: undefined reference to `avcodec_open2(AVCodecContext*, AVCodec const*, AVDictionary**)'
error: undefined reference to `av_frame_alloc()'
error: undefined reference to `av_image_alloc(unsigned char**, int*, int, int, AVPixelFormat, int)'
error: undefined reference to `avformat_alloc_context()'
error: undefined reference to `av_malloc(unsigned long)'
error: undefined reference to `avio_alloc_context(unsigned char*, int, int, void*, int (*)(void*, unsigned char*, int), int (*)(void*, unsigned char*, int), long (*)(void*, long, int))'
error: undefined reference to `avformat_alloc_output_context2(AVFormatContext**, AVOutputFormat*, char const*, char const*)'
error: undefined reference to `avformat_free_context(AVFormatContext*)'
2、原因查找

1> 确认过ffmpeg库链接路径、库名字都对

-L[安装ffmpeg路径]/lib -lavcodec -lavformat -lavutil -lswscale -lpthread -lm -lswresample -lx264

2> 以为是系统以前安装的旧版本ffmpeg库造成干扰,将/usb/lib/x86_64-linux-gnu/下的ffmpeg库删除,还是不行。
3> 认真比对昨天的demo,发现,昨天是用gcc编译、今天是用g++编译。百度后才发现
ffmpeg库的接口都是c函数,其头文件也没有extern "C"的声明,所以在cpp文件里调用ffmpeg函数要加extern “C” 。

3、解决方法

在c++中使用ffmpeg时,需要用extern “C” 声明ffmpeg的头文件。

#ifdef __cplusplus
extern "C" {
#endif

#include <libavutil/mathematics.h>
#include <libavformat/avformat.h>
#include <libavutil/time.h>
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>

#ifdef __cplusplus
} // endof extern "C"
#endif
发布了324 篇原创文章 · 获赞 266 · 访问量 42万+

猜你喜欢

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