关于FFmpeg包错误 LNK2019问题解决方法
错误 LNK2019 无法解析的外部符号 “void __cdecl avcodec_register_all(void)” (?avcodec_register_all@@YAXXZ),该符号在函数 main 中被引用
错误 LNK2019 无法解析的外部符号 “int __cdecl avformat_network_init(void)” (?avformat_network_init@@YAHXZ),该符号在函数 main 中被引用
错误 LNK2019 无法解析的外部符号 “struct AVFormatContext * __cdecl avformat_alloc_context(void)” (?avformat_alloc_context@@YAPEAUAVFormatContext@@XZ),该符号在函数 main 中被引用
错误 LNK2019 无法解析的外部符号 “int __cdecl avformat_open_input(struct AVFormatContext * *,char const *,struct AVInputFormat *,struct AVDictionary * *)” (avformat_open_input@@YAHPEAPEAUAVFormatContext@@PEBDPEAUAVInputFormat@@PEAPEAUAVDictionary@@@Z),该符号在函数 main 中被引用
原因
网上看了很多文章。有说没有lib,dll没有的。还有让我直接注释掉的。我使用的是vs2019+ffmpeg的库。 avcodec_register_all()函数报错。原因是因为这个.h文件原来是用C写的。C++中要是用的话要用下面的方式才可以。
错误代码
#include "myProject.h"
#include <stdio.h>
#include <Windows.h>
#include "libavformat/avformat.h"
int main()
{
AVFormatContext* pFormatCtx;
int i, videoindex;
AVCodecContext* pCodecCtx;
AVCodec* pCodec;
// Init
avcodec_register_all(); //注册常用包
avformat_network_init();//初始化网络
pFormatCtx = avformat_alloc_context(); //分配内存
if (avformat_open_input(&pFormatCtx, "rtmp://58.200.131.2:1935/livetv/gxtv", NULL, NULL) != 0) {
printf("Couldn't open input stream.\n");
return -1;
}
return 0;
}
正确代码
#include "myProject.h"
#include <stdio.h>
#include <Windows.h>
#define __STDC_CONSTANT_MACROS
#ifdef _WIN32
//Windows
extern "C"
{
#include "libavformat/avformat.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavformat/avformat.h>
#ifdef __cplusplus
};
#endif
#endif
int main()
{
AVFormatContext* pFormatCtx;
int i, videoindex;
AVCodecContext* pCodecCtx;
AVCodec* pCodec;
// Init
avcodec_register_all(); //注册常用包
avformat_network_init();//初始化网络
pFormatCtx = avformat_alloc_context(); //分配内存
if (avformat_open_input(&pFormatCtx, "rtmp://58.200.131.2:1935/livetv/gxtv", NULL, NULL) != 0) {
printf("Couldn't open input stream.\n");
return -1;
}
return 0;
}