Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve linker library order problems?

Tags:

c

gcc

linker

I am trying to compile a C program that requires the use of several libraries. The problem is, the order the libraries are linked in causes the compilation to fail.

Is there anyway to get GCC to figure out the correct order, or for me to figure out the correct order without having to try every possibility?

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 am doing it at the moment, is starting off with one, and then adding more libraries as the errors occur, however sometimes it feels like progress and then sometimes it just seems like I hit a dead end.

[edit]The compilation fails because of undefined references[/edit]

like image 908
ArturPhilibin Avatar asked Dec 28 '22 04:12

ArturPhilibin


1 Answers

You have a few options

1) You can add additional calls to your library(s) that have dependencies

2) You can use the --start-group / --end-group options like so:

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 manpage entry describing its use

The specified archives are searched repeatedly until no new undefined references are created. Normally, an archive is searched only once in the order that it is specified on the command line. If a symbol in that archive is needed to resolve an undefined symbol referred to by an object in an archive that appears later on the command line, the linker would not be able to resolve that reference. By grouping the archives, they all be searched repeatedly until all possible references are resolved.

like image 111
SiegeX Avatar answered Jan 05 '23 18:01

SiegeX