3
votes

So I have a very sample code for trying to decode a FFMPEG video stream. My problem is avcodec does not want to link, to do so I made a clean installation of Ubuntu 13.04. I have build ffmpeg from source following the guide here: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

I just want to compile my file. Note that my ubuntu does not have any implementations or header files for avcodec. The command line I use is:

gcc -I/home/USER/ffmpeg_build/include -L/home/USER/ffmpeg_build/lib -lavcodec -o test.exe Downloads/auv/src/dronerosvideo/src/ar2.cpp

/tmp/ccKTprFq.o: In function `fetch_and_decode(int, int, bool)':

ar2.cpp:(.text+0x36e): undefined reference to `avcodec_register_all'

ar2.cpp:(.text+0x378): undefined reference to `av_log_set_level'

ar2.cpp:(.text+0x382): undefined reference to `avcodec_find_decoder'

ar2.cpp:(.text+0x3b1): undefined reference to `avcodec_alloc_context3'

ar2.cpp:(.text+0x3d6): undefined reference to `avcodec_open2'

ar2.cpp:(.text+0x46d): undefined reference to `av_init_packet'

ar2.cpp:(.text+0x50a): undefined reference to `avcodec_decode_video2'

ar2.cpp:(.text+0x534): undefined reference to `av_free_packet'

/tmp/ccKTprFq.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'

collect2: error: ld returned 1 exit status

Just for a sane test if I remove the -L argument compiler says:

/usr/bin/ld: cannot find -lavcodec

Which means that the linker finds the library in /home/USER/ffmpeg_build/lib. Also if we check the library for implementation it exists:

nm ffmpeg_build/lib/libavcodec.a | grep "register_all"
0000000000000000 T avcodec_register_all

Also as advised since it is C++ I have exten "C" around the include of the library.

At this point I'm falling out of any ideas at all, why exactly compilation fails?

1
/usr/bin/ld is the linker. You probably meant : 'the linker finds the library in /home/USER/ffmpeg_build/lib'. - didierc
Yeah correct! I'll modify. - Alex Botev
hello! did you solve it? I have the same problem and I'm stuck in it. - Fatemeh Karimi

1 Answers

3
votes

First, it is C++, so you'll need to use g++ not gcc so that the C++ standard library gets linked. This should get rid of undefined reference to '__gxx_personality_v0'.

Then, the order of libaries to link is actually important. You'll need to specify a library after the object (or source or other library) using it.

Putting it together, a command line like this works (in my tests):

g++ -o test.exe -I$HOME/ffmpeg/include test.cc -L$HOME/ffmpeg/lib -lavcodec

(Actually, depending on how ffmpeg was built, you might need other libraries as well, like pthreads or libx264)

If you got pkg-config installed, it might be possible to just ask that it for proper clags:

# Since you didn't install ffmpeg to a known location, tell pkg-config about that location.
export PKG_CONFIG_PATH=$HOME/ffmpeg/lib/pkgconfig
g++ -o test.exe $(pkg-config -clags libavcodec) test.cc $(pkg-config -libs libavcodec)