8

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)
user2212461
  • 3,105
  • 8
  • 49
  • 87

2 Answers2

7

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

Non-maskable Interrupt
  • 3,841
  • 1
  • 19
  • 26
2

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
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • regarding absolute library paths: isnt your suggested way and -L internally exactly the same? Anyway there was no difference. And the order which I am using seems to be correct in my case. Doesn't the order mean the dependency and they are read backwards? – user2212461 Jul 24 '14 at 21:24