linux调用ffmpeg 遇到的问题

本文介绍了在使用C++编译FFmpeg时遇到的两大常见问题及其解决方案:一是解决宏定义未声明导致的编译错误;二是通过正确使用extern C来避免链接时找不到函数的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ffmpeg用g++编译时的注意事项

1、编译时出现下面错误
libavutil/common.h:185: 错误:‘UINT64_C’ 在此作用域中尚未声明

解决方法:
libavutil/common.h增加如下代码
//user add start
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
//user add end


2、编译链接时找不到对应的函数
undefined reference to `av_register_all()'
undefined reference to `avformat_open_input(AVFormatContext**, char const*, AVInputFormat*, AVDictionary**)'
undefined reference to `avformat_find_stream_info(AVFormatContext*, AVDictionary**)'
undefined reference to `avcodec_find_decoder(AVCodecID)'
undefined reference to `avcodec_open2(AVCodecContext*, AVCodec const*, AVDictionary**)'
undefined reference to `avcodec_alloc_frame()'
undefined reference to `avcodec_alloc_frame()'

解决方法:
用extern "C"{}把头文件包含起来。
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}

参考地址:http://blog.youkuaiyun.com/dgyanyong/article/details/12192041

////////////////////////-----------------------------------///////////////////////////////////////

ffmpeg库的接口都是c函数,其头文件也没有extern "C"的声明,所以在cpp文件里调用ffmpeg函数要注意了。
一般来说,一个用C写成的库如果想被C/C++同时可以使用,那在头文件应该加上
#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
} // endof extern "C"
#endif

如果文件名是main.c,里面调用ffmpeg的接口没有问题;但换成main.cpp后,就会报错 undefined reference。
这是因为.cpp里的符号名不是简单的函数名,而函数后加后缀标志。

例如,代码里有一句av_register_all()调用
int main(int argc, char** argv)
{
 av_register_all();
}
如果该文件名是 main.c,则main.o里的符号为 (用nm命令查看)
$ nm  src/main.o
 U _av_register_all

如果该文件名是 main.cpp,则main.o里的符号为
$ nm src/main.o
  U __Z15av_register_allv
显然,.c和.cpp的函数符号名是不一样的。再看ffmpeg库的符号名
$ nm libavdevice.a | grep register
00000000 T _avdevice_register_all

这里我们就明白了,如果在.cpp里调用av_register_all()在链接时将找到不符号,因为.cpp要求的符号名
和ffmpeg库提供的符号名不一致。

可以这么解决:
extern "C"
{
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}

参考地址:http://blog.sina.com.cn/s/blog_8cfe05150100uhm2.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值