经典错误::error LNK2019:无法解析的外部符号 ,该符号在函数**中被引用

将视频逐帧提取,转换为图片保存下来:

源代码:

#include <cv.h>
#include <highgui.h>

#if 1 //错误一//错误一//错误一//错误一//错误一//错误一//错误一//错误一//错误一//错误一//错误一//错误一
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#endif

#include "libRtsp2RGB24Sdk.h"
#include "libRtsp2RGB24Header.h"

int nFrameWidth=1920, nFrameHeight=1080;
void SaveFrame(AVPicture pFrameRGB, int width, int height)
{
IplImage * dst = cvCreateImage(cvSize(nFrameWidth,nFrameHeight),8,3);;
LPVOID pMem = (LPVOID)pFrameRGB.data[0];

memcpy(dst->imageData,pMem,nFrameWidth*nFrameHeight*3);

cvSaveImage("D:\\pic.jpg",dst);
}

void main()
{
avcodec_register_all();
av_register_all();
avformat_network_init();

AVFormatContext *pFormatCtx;

#if 1 //错误二//错误二//错误二//错误二//错误二//错误二//错误二//错误二//错误二//错误二//错误二//错误二
pFormatCtx = avformat_alloc_context(); //必不可少!
#endif

char *filename = "222.mpg"; // "C:\\Users\\HYZ\\Desktop\\2.avi";

avformat_open_input(&pFormatCtx, filename, NULL, 0);

av_find_stream_info(pFormatCtx);

int videoStream=-1;
for(int i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
videoStream=i;
break;
}
if(videoStream == -1)
{
//sInfo.Format("视频中无视频解码器信息");
//return FALSE;
}

AVCodec * pCodec;
AVCodecContext * pCodecCtx;
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (!pCodec)
{
//sInfo.Format("无法找到视频解码器");
//return FALSE;
}
avcodec_open2(pCodecCtx, pCodec, NULL); // 打开解码器

int frameFinished = 0;

AVFrame *pFrame;
pFrame=avcodec_alloc_frame();

AVPacket avPacket;
av_init_packet(&avPacket);

AVPicture pRGBFrame;
avpicture_alloc(&pRGBFrame,PIX_FMT_RGB24,nFrameWidth,nFrameHeight);

SwsContext * pSwsContext;
pSwsContext = sws_getContext(nFrameWidth, nFrameHeight, pCodecCtx->pix_fmt, nFrameWidth, nFrameHeight,
PIX_FMT_BGR24,SWS_FAST_BILINEAR,NULL,NULL,NULL);

while(av_read_frame(pFormatCtx, &avPacket)>=0)
{
if(avPacket.stream_index==videoStream)
{
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &avPacket);//解码

if(frameFinished>0)
{
sws_scale(pSwsContext, pFrame->data, pFrame->linesize, 0, nFrameHeight, pRGBFrame.data, pRGBFrame.linesize);
}

SaveFrame(pRGBFrame, pCodecCtx->width, pCodecCtx->height); //保存数据

av_free_packet(&avPacket); //释放
}
}
}

 

错误一:

运行后错误提示:

1>111.obj : error LNK2019: 无法解析的外部符号 "void __cdecl av_free_packet(struct AVPacket *)" (?av_free_packet@@YAXPAUAVPacket@@@Z),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "int __cdecl avcodec_decode_video2(struct AVCodecContext *,struct AVFrame *,int *,struct AVPacket const *)" (?avcodec_decode_video2@@YAHPAUAVCodecContext@@PAUAVFrame@@PAHPBUAVPacket@@@Z),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "int __cdecl av_read_frame(struct AVFormatContext *,struct AVPacket *)" (?av_read_frame@@YAHPAUAVFormatContext@@PAUAVPacket@@@Z),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "int __cdecl avpicture_alloc(struct AVPicture *,enum AVPixelFormat,int,int)" (?avpicture_alloc@@YAHPAUAVPicture@@W4AVPixelFormat@@HH@Z),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "void __cdecl av_init_packet(struct AVPacket *)" (?av_init_packet@@YAXPAUAVPacket@@@Z),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "struct AVFrame * __cdecl avcodec_alloc_frame(void)" (?avcodec_alloc_frame@@YAPAUAVFrame@@XZ),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "int __cdecl avcodec_open2(struct AVCodecContext *,struct AVCodec const *,struct AVDictionary * *)" (?avcodec_open2@@YAHPAUAVCodecContext@@PBUAVCodec@@PAPAUAVDictionary@@@Z),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "struct AVCodec * __cdecl avcodec_find_decoder(enum AVCodecID)" (?avcodec_find_decoder@@YAPAUAVCodec@@W4AVCodecID@@@Z),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "int __cdecl av_find_stream_info(struct AVFormatContext *)" (?av_find_stream_info@@YAHPAUAVFormatContext@@@Z),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "int __cdecl avformat_open_input(struct AVFormatContext * *,char const *,struct AVInputFormat *,struct AVDictionary * *)" (?avformat_open_input@@YAHPAPAUAVFormatContext@@PBDPAUAVInputFormat@@PAPAUAVDictionary@@@Z),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "struct AVFormatContext * __cdecl avformat_alloc_context(void)" (?avformat_alloc_context@@YAPAUAVFormatContext@@XZ),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "int __cdecl avformat_network_init(void)" (?avformat_network_init@@YAHXZ),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "void __cdecl av_register_all(void)" (?av_register_all@@YAXXZ),该符号在函数 _main 中被引用
1>111.obj : error LNK2019: 无法解析的外部符号 "void __cdecl avcodec_register_all(void)" (?avcodec_register_all@@YAXXZ),该符号在函数 _main 中被引用
1>C:\Users\HYZ\Desktop\测试环境集\stream22.exe : fatal error LNK1120: 14 个无法解析的外部命令

 

解决方法:注释掉错误一,不包含那些头文件即可。包含太多的文件也会发生无法解析的错误,搞不懂。

 

错误二:

运行到avformat_open_input()函数处停止,而且F11也进不去。

弹出错误提示框:stream22.exe中的*****处有未经处理的异常:0xc0000005:读取位置0xccccccccccc时发生访问冲突

 

解决方法:分配内存空间即可:这类错误一般是内存错误导致,重点排查内存分配。

 

posted @ 2015-10-22 11:29  zeus135  阅读(16257)  评论(0编辑  收藏  举报