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)
{
}
如果该文件名是 main.c,则main.o里的符号为 (用nm命令查看)
$ nm
如果该文件名是 main.cpp,则main.o里的符号为
$ nm src/main.o
显然,.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