Thursday, April 28, 2011

GCC Linkage order

I accidentally learned a new fact today about the GCC toolchain. It is regarding the Linkage order of libraries. Let us see what the GCC manual says

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.

0 comments: