未定义的avcodec_alloc_context参考但ffmpeg链接器顺序是否正确?

use*_*461 7 c++ ffmpeg static-linking libavcodec libavformat

我想构建静态链接到libavcodec和libavformat的静态链接可执行文件.静态ffmpeg库是用以下代码构建的:

./configure --enable-static --enable-gpl --enable-nonfree --disable-vaapi 
     --disable-libopus --prefix=myBuild --disable-swresample
Run Code Online (Sandbox Code Playgroud)

接头设置如下:

g++ -O2 -static -o myBin myBin-myBin.o someotherlibraries.a 
     -L/ffmpeg/myBuild/lib -lavformat -lavcodec -lavutil  -lrt -lm -lpthread -lz
Run Code Online (Sandbox Code Playgroud)

编译时,我收到一条错误消息>: - /

src/ffmpeg/myProgram.cpp:115: error: undefined reference to 'avcodec_alloc_context'
Run Code Online (Sandbox Code Playgroud)

输出nm /ffmpeg/myBuild/lib/libavcodec.a | grep avcodec_alloc_context:

         U avcodec_alloc_context3
         U avcodec_alloc_context3
000003c0 T avcodec_alloc_context3
         U avcodec_alloc_context3
Run Code Online (Sandbox Code Playgroud)

我包含带有extern"C"{}的libavcodec.h,我相信我的静态链接器顺序是正确的.为什么我会收到此错误?是因为这个方法已被弃用了吗?我怎么解决这个问题?

解:

不要用

avCtx = avcodec_alloc_context()
Run Code Online (Sandbox Code Playgroud)

从可能较旧的代码片段,但使用

codec = avcodec_find_decoder(CODEC_ID_XYZ);//for completeness but should be the same as before
avCtx = avcodec_alloc_context3(codec)
Run Code Online (Sandbox Code Playgroud)

Non*_*upt 6

您是否尝试过调用avcodec_alloc_context3?

我遇到没有问题调用avcodec_alloc_context3,分配extradata然后调用avcodec_open2.

链接顺序也应该是-lavutil -lavformat -lavcodec