ffmpeg - compilation problem with g++

2019-04-13 02:52发布

问题:

I am trying to run a program which uses ffmpeg libraries to decode a mp3 file. I am using following options for compiling the program:

g++ -c play.cpp -o play.o
g++ -o play play.o -L/usr/local/lib -lavutil -lavcodec -lavformat -lavdevice \
                   -lavfilter -ldl -lasound -L/usr/lib -lSDL -lpthread -lz -lswscale -lm

But while linking I am gettign following errors:

play.cpp:(.text+0x49): undefined reference to `av_dup_packet(AVPacket*)'
play.cpp:(.text+0x66): undefined reference to `av_malloc(unsigned int)'
play.cpp:(.text+0x324): undefined reference to `avcodec_decode_audio3(AVCodecContext*, short*, int*, AVPacket*)'
play.cpp:(.text+0x387): undefined reference to `av_free_packet(AVPacket*)'

and so on... These reported functions are available in the libavcodec.a etc. which i have already specified with the link options. Can anyone please tell me what could be wrong here or suggest how to approach debugging this?

回答1:

Is this your own program, not the libav standard example? I might be wrong, but if so you probably could forget to specify extern "C" while including libav headers:

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

This can be the problem 'coz you trying to compile sources with C++ compiler and libav (ffmpeg) is compiled using C compiler, so you must mark include headers for library as compiled by C compiler using extern C.

If you do mark includes as extern C already, please post some code chunk from your program to look at..



回答2:

You should make sure with verbose linkin (-Wl,--verbose) that the right version of -lavcodec is chosen, and check with objdump or ldd if the symbols are truly in the library.

You might try switching the order of -l flags, these are very important; however, since play.cpp contains the references to functions in -lavcodec, the flag order already should be right.



标签: c++ ffmpeg