How to solve problems with the library library?

I am trying to compile a C program that requires the use of several libraries. The problem is that the order with which the libraries are linked leads to compilation failure.

In any case, to get GCC to figure out the correct order, or for me to figure out the correct order without trying to try all the possibilities?

mipsel-linux-gcc ffmpeg_mips_test.c -o ffmpeg_mips_test -Wall -v -I/ffmpegMIPS/includeffmpegMIPS/ -L/ffmpegMIPS/libffmpegMIPS/ -lavformat -lavcodec -lavutil -lswscale -lm -lpthread 

The way I do this at the moment starts with one and then adds more libraries as errors occur, however sometimes it seems like progress, and sometimes it sometimes seems like I'm at a dead end.

[edit] Compilation failed due to undefined links [/ edit]

+4
source share
2 answers

You have several options.

1) You can add additional calls to your libraries that have dependencies

2) You can use the parameters --start-group / --end-group , for example:

 mipsel-linux-gcc ffmpeg_mips_test.c -o ffmpeg_mips_test -Wall -v -I/ffmpegMIPS/includeffmpegMIPS/ -L/ffmpegMIPS/libffmpegMIPS/ -Wl,--start-group -lavformat -lavcodec -lavutil -lswscale -Wl,--end-group -lm -lpthread 

Here is the ld quotation mark describing its use

Selected archives until a new undefined link is created. Typically, an archive is executed only once so that it is indicated on the command line. If a symbol in this archive is needed for an undefined symbol, a designated object in the archive that appears later on the command line, the linker will not be able to solve this problem. Help. By combining archives, they will all be searched until all possible links are resolved.

+8
source

Using -lpthread is deprecated. You must use -pthread and gcc will generate any parameters and libraries needed to support pthread.

As for others, their proper disposal is simply a matter of understanding the dependencies between them. If A depends on B , -lA should appear before -lB on the command line. In your case, -lm is a system mathematical library (which is separated only from the main libc for stupid causes of fatigue) and does not depend on anything else, so it should always be at the end of the command line. -lavutil is a library of utility functions used by ffmpeg and its included libraries, so it should appear after all other ffmpeg . -lswscale is an image scaling library that might be needed for other libraries, so I would put it after -lavformat and -lavcodec , but before -lavutil if it needs functions from -lavutil .

Finally, in the ffmpeg world, codecs are considered fundamental, and containers are considered a layer on top of them (rather, the opposite of some frameworks), so -lavformat depends on -lavcodec . So the final order in your example should be:

 -lavformat -lavcodec -lswscale -lavutil -lm 

And -pthread can go anywhere on the command line; it usually calls gcc to put the hidden -lpthread at the end, but can do different things as needed on different systems.

+2
source

All Articles