ffmpeg 静态库使用,undefined reference错误

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

猜你喜欢

转载自blog.csdn.net/baidu_38172402/article/details/80789069
今日推荐