Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to avcodec_alloc_context but ffmpeg linker order correct?

I want to build a statically linked executable statically linked to libavcodec and libavformat. The static ffmpeg library was build with:

./configure --enable-static --enable-gpl --enable-nonfree --disable-vaapi 
     --disable-libopus --prefix=myBuild --disable-swresample

The linkers are set as follows:

g++ -O2 -static -o myBin myBin-myBin.o someotherlibraries.a 
     -L/ffmpeg/myBuild/lib -lavformat -lavcodec -lavutil  -lrt -lm -lpthread -lz

When compiling, I get ONLY ONE error message >:-/

src/ffmpeg/myProgram.cpp:115: error: undefined reference to 'avcodec_alloc_context'

Output of 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

I include libavcodec.h with extern "C" {} and I believe my static linker order is correct. Why do I get this error? Is it because this method has been deprecated? How can I solve this?

SOLUTION:

Dont use

avCtx = avcodec_alloc_context()

from maybe older code snippets, but use

codec = avcodec_find_decoder(CODEC_ID_XYZ);//for completeness but should be the same as before
avCtx = avcodec_alloc_context3(codec)
like image 350
user2212461 Avatar asked Jul 22 '14 10:07

user2212461


2 Answers

Did you try to call avcodec_alloc_context3 instead?

I encounter no issue calling avcodec_alloc_context3, allocate extradata then call avcodec_open2.

Also the link order should be -lavutil -lavformat -lavcodec

like image 52
Non-maskable Interrupt Avatar answered Sep 22 '22 08:09

Non-maskable Interrupt


if I recall correctly we also had problems with this and the solution was that you have to specifically add the libavcodec.a (together with full path) and the other ffmpeg static libraries to the g++ linking step. See if it works this way.

Also, the order of the libraries is important. I don't have the old makefiels anymore, but maybe recall that libavutil should be the first in the list.

So your linking command should be something like:

g++ -O2 -static -o myBin myBin-myBin.o someotherlibraries.a 
 /ffmpeg/myBuild/lib/libavutil.a 
 /ffmpeg/myBuild/lib/libavformat.a 
 /ffmpeg/myBuild/lib/libavcodec.a 
  -lrt -lm -lpthread -lz
like image 30
Ferenc Deak Avatar answered Sep 25 '22 08:09

Ferenc Deak