I've seen this article and learned that:
- Directories specified on the command line with
-L
option are searched before the default directories.
- The directories specified in
-L
are searched in the order in which they are specified on the command line.
The question is: Is there an search order for the default directories?
For example, if I run this command:
$ gcc -Xlinker --verbose 2>/dev/null | grep SEARCH | sed 's/SEARCH_DIR("=\?\([^"]\+\)"); */\1\n/g' | grep -vE '^$'
(command copied from this article)
it prints out /usr/local/lib
before /usr/lib
in my machine (Ubuntu 16.04, 64-bit, gcc 5.4.0). In this case, will /usr/local/lib
be searched before /usr/lib
?
From the binutils ld manual section 3.4.2 Commands Dealing with Files:
SEARCH_DIR(path)
The SEARCH_DIR command adds path to the list of paths where ld looks for archive libraries. Using SEARCH_DIR(path) is exactly like using `-L path' on the command line (see Command Line Options). If both are used, then the linker will search both paths. Paths specified using the command line option are searched first.
So, yes, as the default directories are given in the default linker script using this SEARCH_DIR()
command, they will be searched in the order the SEARCH_DIR()
commands appear.
E.g., in my mingw
installation, the default linker script starts like this:
/* Default linker script, for normal executables */
/* Copyright (C) 2014-2017 Free Software Foundation, Inc.
Copying and distribution of this script, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. */
OUTPUT_FORMAT(pei-i386)
SEARCH_DIR("=/mingw32/i686-w64-mingw32/lib");
SEARCH_DIR("=/mingw32/lib");
SEARCH_DIR("=/usr/local/lib");
SEARCH_DIR("=/lib");
SEARCH_DIR("=/usr/lib");
--> A library in /usr/local/lib
can override libraries in /lib
and /usr/lib
, but not libraries provided by mingw
itself.