gcc or g++ path search order on default directories when linking libraries

前提是你 提交于 2019-12-24 06:34:06

问题


I've seen this article and learned that:

  1. Directories specified on the command line with -L option are searched before the default directories.
  2. 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?


回答1:


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.



来源:https://stackoverflow.com/questions/44401829/gcc-or-g-path-search-order-on-default-directories-when-linking-libraries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!