FFmpeg编译,以及常见错误解决

编译

这里强烈推荐官网的编译指南,少走一些环境问题,版本问题的弯路
FFmpeg编译指南

这里不多说,照着做基本不会有太大问题

Demo测试

.
├── build.sh
├── include
├── lib
├── share
├── test
└── testFFmpeg.c

测试代码 testFFmpeg.c

//testFFmpeg.c
#include <stdio.h>
#include <libavcodec/avcodec.h>

int main(int argc, char *argv[])
{
    printf("hello ffmpeg\n");
    avcodec_register_all();
    return 0;
}

开开心心的来编译,结果

~/ffmpeg_build# sh -x build.sh 
+ pwd
+ FFMPEG=/root/ffmpeg_build
+ gcc -I/root/ffmpeg_build/include/ -L/root/ffmpeg_build/lib/ -lavformat -lavcodec -lavutil -lswresample -lz -llzma -lpthread -lm testFFmpeg.c -o test
testFFmpeg.c: In function ‘main’:
testFFmpeg.c:8:5: warning: ‘avcodec_register_all’ is deprecated [-Wdeprecated-declarations]
    8 |     avcodec_register_all();
      |     ^~~~~~~~~~~~~~~~~~~~
In file included from testFFmpeg.c:3:
/root/ffmpeg_build/include/libavcodec/avcodec.h:2767:6: note: declared here
 2767 | void avcodec_register_all(void);
      |      ^~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccQQlEf5.o: in function `main':
testFFmpeg.c:(.text+0x20): undefined reference to `avcodec_register_all'

错误已经很明显了,undefined reference to avcodec_register_all

说法1:extern
网上绝大多数说法是,需要extern,因为ffmpeg是纯C语言编写的

extern "C" {
#include <libavcodec/avcodec.h>
}

但我不是C++,所以问题没有解决

说法2:
添加更多链接

抱着试试的心态,还是不行.

最后看了这ffmpeg移植到dm365上,遇到undefined reference错误文章
用下面命令重新编译了一下,居然成功了

gcc -o test testFFmpeg.c $(pkg-config --libs --cflags libavcodec)

说到底还是包的依赖问题,编译真的复杂,搞了一下午

posted @ 2021-04-17 17:31  梦过无声  阅读(1996)  评论(0编辑  收藏  举报