I have written a Python C module (just ffmpeg.c which depends on some FFmpeg libs and other libs) and I am wondering how to link.
I’m compiling with:
cc -std=c99 -c ../ffmpeg.c -I /usr/include/python2.7 -g
I’m trying to link right now with:
ld -shared -o ../ffmpeg.so -L/usr/local/lib -lpython2.7 -lavutil -lavformat -lavcodec -lswresample -lportaudio -lchromaprint ffmpeg.o -lc
There is no error. However, when I try to import ffmpeg in Python, I get:
ImportError: ./ffmpeg.so: undefined symbol: avio_alloc_context
Maybe this is already correct. I checked the resulting ffmpeg.so with ldd and it partly links to a wrong FFmpeg. This is strange however because of the -L/usr/local/lib which should take precedence over the default. Maybe because my custom installed FFmpeg (in /usr/local/lib) has for some reason only installed static *.a libs and *.so files have precedence over *.a files.
You should put the libraries that you’re linking to after the
.ofile; i.e.:The linker is dumb, and will not link in code from static libraries that it doesn’t think are needed until a dependency arises i.e. the use of
avio_alloc_contexthappens inffmpeg.o, and because it’s not listed after the use of the library, then the linker will not consider the code in the library as needed, so it doesn’t get linked in – this is the biggest reason why linking using.afiles fails.You can also use
--start-groupand--end-grouparound all the files that you are linking – this allows you to link static libraries that have cross dependencies that just seem impossible to resolve through other means:using
.afiles is a little bit trickier than.sofiles, but these two items generally will work around any issues you have when linking.