linux undefined reference ffmpeg

最近研究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)

locate -b libavcodec.so 
/home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/amd64/usr/lib/x86_64-linux-gnu/libavcodec.so.53 
/home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/amd64/usr/lib/x86_64-linux-gnu/libavcodec.so.53.35.0 
/home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/libavcodec.so.53 
/home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/libavcodec.so.53.35.0 
/home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.53 
/home/kenn/.local/share/Steam/ubuntu12_32/steam-runtime/i386/usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.53.35.0 
/usr/lib/i386-linux-gnu/libavcodec.so 
/usr/lib/i386-linux-gnu/libavcodec.so.53 
/usr/lib/i386-linux-gnu/libavcodec.so.53.35.0 
/usr/lib/i386-linux-gnu/libavcodec.so.54 
/usr/lib/i386-linux-gnu/libavcodec.so.54.92.100 
/usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so 
/usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.53 
/usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.53.35.0 
/usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.54 
/usr/lib/i386-linux-gnu/i686/cmov/libavcodec.so.54.92.100 
/usr/local/lib/libavcodec.so 
/usr/local/lib/libavcodec.so.55 
/usr/local/lib/libavcodec.so.55.58.103 


不仅仅是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

TEMPLATE = app 
CONFIG += console 
CONFIG -= qt 

SOURCES += main.c 

INCLUDEPATH +=  . /usr/local/include 
LIBS += -L./usr/local/lib/  -lavformat -lavdevice  -lavcodec -lavutil  -lavfilter \ 
-lpostproc  -lswresample -lswscale 
LIBS += -L./usr/lib/x86_64-linux-gnu/ -lva -lva-x11 -lva -lxcb -lxcb-shm\ 
-lxcb -lX11 -lasound -lSDL -lxvidcore -lx264 -lpthread -ltheoraenc  \ 
-ltheoradec -logg -lopencore-amrwb -lopencore-amrnb -lmp3lame -lfaac\ 
-lm -lbz2 -lz -pthread -lrt 


main.c

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include "libavcodec/avcodec.h" 
#include "libavformat/avformat.h" 
#include "libswscale/swscale.h" 
#include "libavutil/pixfmt.h" 
#include "libavutil/frame.h" 

int main() 

printf("Hello Pepper Robot!\n"); 
AVFormatContext *pFormatCtx;//封装格式上下文结构体,也是统领全局的结构体,保存了视频文件封装格式相关信息。 
int i, videoindex; 
AVCodecContext  *pCodecCtx;//编码器上下文结构体,保存了视频(音频)编解码相关信息。 
AVCodec *pCodec;//每种视频(音频)编解码器(例如H.264解码器)对应一个该结构体。 
AVFrame *pFrame,*pFrameYUV;//存储一帧解码后像素(采样)数据。 
uint8_t *out_buffer; 
AVPacket *packet;//储存一帧压缩编码数据 
//int y_size; 
int ret, got_picture; 
struct SwsContext *img_convert_ctx; 
//输入文件路径 
char filepath[]="Titanic.ts"; 

int frame_cnt; 

av_register_all();//初始化ffmpeg库,如果系统的ffmpeg没有配置好,这里会出错 
avformat_network_init(); 
//使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input()。 
//其中打开网络流的话,前面要加上函数avformat_network_init()。 
pFormatCtx = avformat_alloc_context();//结构体初始化 
//打开输入视频文件 
if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){ 
printf("Couldn't open input stream.\n"); 
return -1; 
}//获取视频文件的信息 
if(avformat_find_stream_info(pFormatCtx,NULL)<0){ 
printf("Couldn't find stream information.\n"); 
return -1; 

//遍历文件的各个流,找到第一个视频流,并记录该流的编码信息 
videoindex=-1; 
for(i=0; i<pFormatCtx->nb_streams; i++) 
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){ 
videoindex=i; 
break; 

if(videoindex==-1){ 
printf("Didn't find a video stream.\n"); 
return -1; 

pCodecCtx=pFormatCtx->streams[videoindex]->codec; 
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);//查找解码器 
if(pCodec==NULL){ 
printf("Codec not found.\n"); 
return -1; 
}//打开解码器 
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){ 
printf("Could not open codec.\n"); 
return -1; 

/*
* 在此处添加输出视频信息的代码
* 取自于pFormatCtx,使用fprintf()
*/ 
pFrame=av_frame_alloc();//分配一个帧指针,指向解码后的原始帧 
pFrameYUV=av_frame_alloc();//分配一个帧指针,指向存放转换成YUV后的帧 
out_buffer=(uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)); 
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height); 
packet=(AVPacket *)av_malloc(sizeof(AVPacket)); 
//Output Info----------------------------- 
printf("--------------- File Information ----------------\n"); 
av_dump_format(pFormatCtx,0,filepath,0); 
printf("-------------------------------------------------\n"); 
//根据编码信息设置渲染格式 
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, 
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); 

frame_cnt=0; 
//从输入文件读取一帧压缩数据 
while(av_read_frame(pFormatCtx, packet)>=0){ 
if(packet->stream_index==videoindex){ 
/*
* 在此处添加输出H264码流的代码
* 取自于packet,使用fwrite()
*/ 
//解码一帧压缩数据 
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); 
if(ret < 0){ 
printf("Decode Error.\n"); 
return -1; 

if(got_picture){//视频像素数据格式转换 
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, 
pFrameYUV->data, pFrameYUV->linesize); 
printf("Decoded frame index: %d\n",frame_cnt); 

/*
* 在此处添加输出YUV的代码
* 取自于pFrameYUV,使用fwrite()
*/ 

frame_cnt++; 



av_free_packet(packet); 

sws_freeContext(img_convert_ctx); 

av_frame_free(&pFrameYUV); 
av_frame_free(&pFrame); 
avcodec_close(pCodecCtx);//关闭解码器 
avformat_close_input(&pFormatCtx);//关闭视频输入 

return 0; 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值