2.7.1 Link order of libraries
The traditional behavior of linkers is to search for
external functions from left to right in the libraries specified on the
command line. This means that a library containing the definition of a
function should appear after any source files or object files which use
it. This includes libraries specified with the short-cut -l
option, as shown in the following command:
$ gcc -Wall calc.c -lm -o calc (correct order)
With some linkers the opposite ordering (placing the -lm
option before the file which uses it) would result in an error,
$ cc -Wall -lm calc.c -o calc (incorrect order)
main.o: In function `main':
main.o(.text+0xf): undefined reference to `sqrt'
because there is no library or object file containing sqrt
after
'calc.c'. The option -lm
should appear after the file
'calc.c'.
When several libraries are being used, the same convention should be
followed for the libraries themselves. A library which calls an
external function defined in another library should appear before the
library containing the function.
For example, a program 'data.c' using the GNU Linear Programming
library 'libglpk.a', which in turn uses the math library
'libm.a', should be compiled as,
$ gcc -Wall data.c -lglpk -lm
since the object files in 'libglpk.a' use functions defined in
'libm.a'.
Most current linkers will search all libraries, regardless
of order, but since some do not do this it is best to follow the
convention of ordering libraries from left to right.
This is worth keeping in mind if you ever encounter unexpected problems
with undefined references, and all the necessary libraries appear to be
present on the command line.