Strange behaviour of gcc and math.h?-Collection of common programming errors
Yes, it is normal. For many linkers, the order in which you specify the object files and the libraries matters.
To quote “An Introduction to GCC – for the GNU compilers gcc and g++”:
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)
This behaviour is common, but is by no means universal. When in doubt, it is best to consult your linker’s manual. For example, on my Ubuntu system man ld
states that:
-l namespec
--library=namespec
...
The linker will search an archive only once, at the location where
it is specified on the command line. If the archive defines a
symbol which was undefined in some object which appeared before the
archive on the command line, the linker will include the
appropriate file(s) from the archive. However, an undefined symbol
in an object appearing later on the command line will not cause the
linker to search the archive again.
In other words, this linker does behave in the manner described in the gcc
book.
Originally posted 2013-11-09 20:02:36.