转载自:https://blog.csdn.net/bobsweetie/article/details/50933605

最近研究ffmpeg的时候遇到不少问题,我的系统环境ubuntu12.04,开发环境Qt,总结如下:

在ubuntu下编译好ffmpeg静态库(.a)文件后(参考我的另一篇文章),在/usr/local/bin下生成了可以运行的文件,在/usr/local/include下是头文件,/usr/local/lib下生成了八个.a文件。

新建Qt的工程,在.pro中添加头文件路径和静态库的路径,编译时出现如下问题:

undefined reference to 'av_frame_alloc'

undefined reference to 'av_frame_free'

查了很多资料,总结了以下几种可能的原因:

1、链接时缺少相关的目标文件(.o)
2、链接时缺少相关的库文件(.a/.so)
3、链接的库文件中又使用了另一个库文件
4、多个库文件链接顺序问题
5、在c++代码中链接c语言的库,没有添加extern "C";
6、ffmpeg过时;

7、头文件和你的库文件不匹配(你安装了多个版本的ffmpeg时);

8、路径、需要的库包含不全。

对于第七类问题,参考https://github.com/MaartenBaert/ssr/issues/215,执行locate -b libavcodec.so你会发现有许多的对应的动态文件输出,当你在.pro添加库文件路径时,并不能确定链接的是动态库还是静态库,我添加路径的时候是静态库(.a)的路径,但是实际上链接的确是动态库的文件(.so)。

那么为什么ubuntu不链接我在.pro指定的静态库文件呢?这是因为/lib和/usr/lib是linux系统的默认库文件搜索路径,如果需要添加其他的搜索路径,需要添加环境变量LD_LIBRARY_PATH,或者修改/etc/ld.so.conf。我的静态库文件在/usr/local/下面,这个路径也添加进了/etc/ld.so.conf,所以可以默认搜索到,但是按照搜索顺序,首先搜索到一个不知什么时候安装的ffmpeg动态库文件,因此我必须删掉这些文件。

因此我的库文件默认搜索路径是包括/usr/local/lib的,只要放进该目录不需要指定具体路径编译时就可以找到。

我发现之前链接都是动态库,于是我删除了动态库(.so),只留下在/usr/local/lib下的静态库(.a)

[objc] view plaincopy
  1. <span style="font-size:12px;">locate -b libavcodec.so
  2. /home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/amd64/usr/lib/x86_64-linux-gnu/libavcodec.so.53
  3. /home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/amd64/usr/lib/x86_64-linux-gnu/libavcodec.so.53.35.0
  4. /home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/libavcodec.so.53
  5. /home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/libavcodec.so.53.35.0
  6. /home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.53
  7. /home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.53.35.0
  8. /usr/lib/i386-linux-gnu/libavcodec.so
  9. /usr/lib/i386-linux-gnu/libavcodec.so.53
  10. /usr/lib/i386-linux-gnu/libavcodec.so.53.35.0
  11. /usr/lib/i386-linux-gnu/libavcodec.so.54
  12. /usr/lib/i386-linux-gnu/libavcodec.so.54.92.100
  13. /usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so
  14. /usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.53
  15. /usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.53.35.0
  16. /usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.54
  17. /usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.54.92.100
  18. /usr/local/lib/libavcodec.so
  19. /usr/local/lib/libavcodec.so.55
  20. /usr/local/lib/libavcodec.so.55.58.103</span>

不仅仅是libavcodec,其他库包括libavformat, libavutil, libswscale, libswresample, libavresample, libavfilter也要删除它的动态库。

最后你执行locate -b 【lib_name.a】时,只看到一个路径,就是我的静态库路径。(ps:删掉之后需要关机,断充电电源,然后启动之后才会生效,否则,马上执行locate还是会看到其它的对应的lib_name.a)。如下:

locate -b libavcodec.a
/usr/local/lib/libavcodec.a

这下没问题了,没有其它动态库的路径了。头文件和静态库文件应该能匹配到一起了。编译后又出现了第四类问题和第八类问题,出现了很多的undefined reference错误,但是前面的undefined referenceto 'av_frame_alloc',undefined reference to 'av_frame_free' 错误已经没有了。更改库的链接顺序,并且根据ffmpeg的 pkg-config填上其他的依赖库:

.pro

[objc] view plaincopy
  1. <span style="font-size:12px;">TEMPLATE = app
  2. CONFIG += console
  3. CONFIG -= qt
  4. SOURCES += main.c
  5. INCLUDEPATH +=  . /usr/local/include
  6. LIBS += -L./usr/local/lib/  -lavformat -lavdevice  -lavcodec -lavutil  -lavfilter \
  7. -lpostproc  -lswresample -lswscale
  8. LIBS += -L./usr/lib/x86_64-linux-gnu/ -lva -lva-x11 -lva -lxcb -lxcb-shm        \
  9. -lxcb -lX11 -lasound -lSDL -lxvidcore -lx264 -lpthread -ltheoraenc      \
  10. -ltheoradec -logg -lopencore-amrwb -lopencore-amrnb -lmp3lame -lfaac    \
  11. -lm -lbz2 -lz -pthread -lrt</span>

main.c

[objc] view plaincopy
  1. <span style="font-size:12px;">#include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include "libavcodec/avcodec.h"
  5. #include "libavformat/avformat.h"
  6. #include "libswscale/swscale.h"
  7. #include "libavutil/pixfmt.h"
  8. #include "libavutil/frame.h"
  9. int main()
  10. {
  11. printf("Hello Pepper Robot!\n");
  12. AVFormatContext *pFormatCtx;//封装格式上下文结构体,也是统领全局的结构体,保存了视频文件封装格式相关信息。
  13. int i, videoindex;
  14. AVCodecContext  *pCodecCtx;//编码器上下文结构体,保存了视频(音频)编解码相关信息。
  15. AVCodec         *pCodec;//每种视频(音频)编解码器(例如H.264解码器)对应一个该结构体。
  16. AVFrame *pFrame,*pFrameYUV;//存储一帧解码后像素(采样)数据。
  17. uint8_t *out_buffer;
  18. AVPacket *packet;//储存一帧压缩编码数据
  19. //int y_size;
  20. int ret, got_picture;
  21. struct SwsContext *img_convert_ctx;
  22. //输入文件路径
  23. char filepath[]="Titanic.ts";
  24. int frame_cnt;
  25. av_register_all();//初始化ffmpeg库,如果系统的ffmpeg没有配置好,这里会出错
  26. avformat_network_init();
  27. //使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input()。
  28. //其中打开网络流的话,前面要加上函数avformat_network_init()。
  29. pFormatCtx = avformat_alloc_context();//结构体初始化
  30. //打开输入视频文件
  31. if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
  32. printf("Couldn't open input stream.\n");
  33. return -1;
  34. }//获取视频文件的信息
  35. if(avformat_find_stream_info(pFormatCtx,NULL)<0){
  36. printf("Couldn't find stream information.\n");
  37. return -1;
  38. }
  39. //遍历文件的各个流,找到第一个视频流,并记录该流的编码信息
  40. videoindex=-1;
  41. for(i=0; i<pFormatCtx->nb_streams; i++)
  42. if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
  43. videoindex=i;
  44. break;
  45. }
  46. if(videoindex==-1){
  47. printf("Didn't find a video stream.\n");
  48. return -1;
  49. }
  50. pCodecCtx=pFormatCtx->streams[videoindex]->codec;
  51. pCodec=avcodec_find_decoder(pCodecCtx->codec_id);//查找解码器
  52. if(pCodec==NULL){
  53. printf("Codec not found.\n");
  54. return -1;
  55. }//打开解码器
  56. if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
  57. printf("Could not open codec.\n");
  58. return -1;
  59. }
  60. /*
  61. * 在此处添加输出视频信息的代码
  62. * 取自于pFormatCtx,使用fprintf()
  63. */
  64. pFrame=av_frame_alloc();//分配一个帧指针,指向解码后的原始帧
  65. pFrameYUV=av_frame_alloc();//分配一个帧指针,指向存放转换成YUV后的帧
  66. out_buffer=(uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
  67. avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
  68. packet=(AVPacket *)av_malloc(sizeof(AVPacket));
  69. //Output Info-----------------------------
  70. printf("--------------- File Information ----------------\n");
  71. av_dump_format(pFormatCtx,0,filepath,0);
  72. printf("-------------------------------------------------\n");
  73. //根据编码信息设置渲染格式
  74. img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
  75. pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
  76. frame_cnt=0;
  77. //从输入文件读取一帧压缩数据
  78. while(av_read_frame(pFormatCtx, packet)>=0){
  79. if(packet->stream_index==videoindex){
  80. /*
  81. * 在此处添加输出H264码流的代码
  82. * 取自于packet,使用fwrite()
  83. */
  84. //解码一帧压缩数据
  85. ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
  86. if(ret < 0){
  87. printf("Decode Error.\n");
  88. return -1;
  89. }
  90. if(got_picture){//视频像素数据格式转换
  91. sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
  92. pFrameYUV->data, pFrameYUV->linesize);
  93. printf("Decoded frame index: %d\n",frame_cnt);
  94. /*
  95. * 在此处添加输出YUV的代码
  96. * 取自于pFrameYUV,使用fwrite()
  97. */
  98. frame_cnt++;
  99. }
  100. }
  101. av_free_packet(packet);
  102. }
  103. sws_freeContext(img_convert_ctx);
  104. av_frame_free(&pFrameYUV);
  105. av_frame_free(&pFrame);
  106. avcodec_close(pCodecCtx);//关闭解码器
  107. avformat_close_input(&pFormatCtx);//关闭视频输入
  108. return 0;
  109. }</span>

参考:http://blog.csdn.net/chinazjn/article/details/7954984。

2016.4.8

手贱,ffmpeg又出现很多undefined reference错误,解决办法是重新编译了ffmpeg,

发现删除了libavcodec.so.53,libavutil.so.51,libswscale.so.2 等动态库之后,mplayer没法使用了,movie player更别说。

只好重新安装好这些依赖库文件。之后又安装了SDL2.0版本的库,重新编译文件,表示没有任何问题了。

ffmpeg 静态库使用,undefined reference错误相关推荐

  1. 【FFMPEG】【ARM-Linux开发】 ffmpeg 静态库使用,undefined reference错误

    原文:http://blog.csdn.net/chinazjn/article/details/7954984 ffmpeg移植到dm365上,遇到undefined reference错误: GA ...

  2. ffmpeg静态库的裁剪

    直接对ffmpeg的源码进行编译链接生成的静态库非常大,仅仅avcodec就有几十兆.因此根据我们自己的需求对ffmpeg进行裁剪是非常必要的. 先不说具体的,直接上代码.在ffmpeg源码的根目录下 ...

  3. VC使用mingw32编译ffmpeg静态库所需文件(一),ffmpegshim.c

    VC使用mingw32编译ffmpeg静态库所需文件(一),ffmpegshim.c 哈哈,这是我从一些项目里面看到的,这些函数是ffmpeg.a缺少的函数.估计大家会用得上. 当然我移植的的项目都不 ...

  4. 关于 海思平台sample的demo中添加ffmpeg静态库(.a)报错误undefined reference toavpriv_pix_fmt_hps_avi等错误 的解决方法

    若该文为原创文章,转载请注明原文出处 本文章博客地址:https://hpzwl.blog.csdn.net/article/details/123482452 红胖子(红模仿)的博文大全:开发技术集 ...

  5. MinGW下编译ffmpeg静态库给Visual C++使用

    首先推荐 http://ffmpeg.zeranoe.com/builds/, 这里已经有编译好的动态连接库.可惜上面没静态链接库.我也试过 DLL2Lib, 但是无法连接LIBCMT库,只能使用MS ...

  6. android ffmpeg 静态库,在CentOS中编译FFmpeg for Android静态库(含fdk aac,x264)

    本文可以编译出集成了x264和fdk_aac的库,而且支持neon 下载源码: fdk_aac目前最新版是0.1.4,详见其ChangeLog,此处用的是0.1.1版 ffmpeg-2.7.2 解压后 ...

  7. 链接静态库时__imp_前缀错误

    visual studio 中链接静态库, 出现找不到 __imp_函数名 的错误, extern "C"也增加了 找到一段 Logically, the next questio ...

  8. 小心DLL链接静态库时的内存错误

    最近写的模块,在独立的应用程序中测试是没问题的,但把它装配成DLL后,再在另一个应用程序中调用时却出现了内存错误.程序的模块链接关系大概是这样的: module就是我所写的模块,在这里被封装为DLL, ...

  9. libiconv库编译undefined reference to `aliases_lookup‘

    换了个编译工具编译 libiconv发现undefined reference to `aliases_lookup'问题,解决办法是在makefile的编译选项中添加-std=gnu89 举例 CF ...

最新文章

  1. Linux iptables防火墙设置与NAT服务配置
  2. stm32cubeide烧写程序_stm32mp157 Cortex M4开发篇:stm32CubeIDE开发环境搭建
  3. optee中TA的堆的分配
  4. Shell(13)——find
  5. delphi7存储过程传入数组_数据结构线性表之顺序存储 类的封装
  6. linux的自定义input,linux键值到Android键值的转换与自定义
  7. 机器学习入门系列(2)--如何构建一个完整的机器学习项目(一)
  8. 各种分页存储过程 (转)
  9. 【分布式训练】单机多卡—PyTorch
  10. python-PyQuery详解
  11. 使用Excel背单词-高效-简单
  12. js json stringify
  13. 什么叫单模光纤_单模光纤和多模光纤的区别,以及作用是什么?
  14. jca 实例 java_采用jca分析javacore文件示例
  15. oracle的隔离级别
  16. 张三的奶牛踩花:C++用贪心法解POJ3262_Protecting the Flowers问题
  17. PC机安装Mac OS X Snow Leopard各硬件兼容列表
  18. SAP T-code
  19. 阻止迅雷播放器暂停广告
  20. Hive学习笔记(1)基本操作

热门文章

  1. Python3标准库:asyncio异步I/O、事件循环和并发工具
  2. java TCP 从客户端键入信息 循环接收发送 示例
  3. JavaScript -- arguments、apply 、call、bind
  4. 【css】响应式布局 @media媒介 适配平板手机
  5. C 语言实例 - 输出九九乘法口诀表
  6. 万物皆可爬系列查看翻页翻到最后是什么
  7. web开发应届生入职_我如何从全职妈妈着手完成第一份Web开发人员工作
  8. spring架构初学者_完整的厨师和基础架构初学者指南
  9. github 和git_Google编码文档:Git和GitHub
  10. mysql服务启动失败 Starting MySQL. ERROR! The server quit without updating PID file