Linking with ffmpeg's libav

Every single tutorial linked from ffmpeg's documentation suggests using simple library linking switches when compiling against libav, for example:


gcc -o main.o main.c -lavcodec -lavformat

but for whatever the reason, that didn't work in my case. I was just getting a bunch of undefined references to the functions that I was using in my code. Horrible.

After browsing and searching for hours I eventually stumbled upon a post in libav mailing list* that suggested the use of pkg-config, so I typed this in:


gcc -o main.o main.c `pkg-config --cflags --libs libavformat libswscale`

and voilà, my program was compiled with libavformat and libswscale in it!

It is pretty similar to SDL's sdl-config, and thankfully you can even use both at the same time:


gcc -o main.o main.c `pkg-config --cflags --libs libavformat libswscale` `sdl-config --cflags --libs`

The only thing I have noticed is that this is statically linking and therefore entirely including those libraries into the executable, so I'm ending with an executable which is 40 Megabytes in size. Not that I am worried about statically linking, but I wonder if it's possible to only include the functions it's really using or needs.

Bonus: libav with C++

By the way, now that we are on helping people so that they don't waste an entire afternoon looking for info, if you want to use libav in a C++ program, be sure to surround the includes with the traditional extern C block, so that C++ doesn't mangle what it doesn't have to mangle. For example:


#ifdef __cplusplus
extern "C"
{
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libswscale/swscale.h>
}
#endif

The sublibraries you include depend entirely on what you want to do in your project. I.e. you could include libavcodec or not, libswscale or not, and so on.

*By the way, let me use this opportunity to bash against mailing lists as the main support asset in a project. Nowadays they are probably the worst idea ever, right after newsgroups, of course. I tried posting a message to the libav list and it was rejected, days after, because "non members cannot post to the list". Hooray for accessibility, proper sorting of topics, searchability and etc.