c++ - 链接库时默认目录上的 gcc 或 g++ 路径搜索顺序

标签 c++ c gcc linker g++

我看过 this文章并了解到:

  1. 在默认目录之前搜索命令行上使用 -L 选项指定的目录。
  2. -L 中指定的目录按照它们在命令行中指定的顺序进行搜索。

问题是:默认目录有搜索顺序吗?

例如,如果我运行这个命令:

$ gcc -Xlinker --verbose  2>/dev/null | grep SEARCH | sed 's/SEARCH_DIR("=\?\([^"]\+\)"); */\1\n/g'  | grep -vE '^$'

(从 this article 复制的命令)

在我的机器(Ubuntu 16.04、64 位、gcc 5.4.0)中,它在 /usr/lib 之前打印出 /usr/local/lib。这样的话,/usr/local/lib会先于/usr/lib搜索吗?

最佳答案

来自binutils ld manual3.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.

所以,是的,因为默认目录是在默认链接器脚本中使用此SEARCH_DIR() 命令给出的,所以它们将按照 的顺序进行搜索SEARCH_DIR() 命令出现。

例如,在我的 mingw 安装中,默认的链接器脚本是这样启动的:

/* 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");

--> /usr/local/lib 中的库可以覆盖 /lib/usr/lib 中的库,但不能mingw 本身提供的库。

关于c++ - 链接库时默认目录上的 gcc 或 g++ 路径搜索顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44401829/

相关文章:

c++ - 持久邻接图索引实现

在 R (Windows) 中从 C 创建一个 dll 动态库

c++ - 可移植浮点变量模板

c - 未定义对 `pow' 和 `floor' 的引用

c++ - 重载 vector<T> 的输出流运算符

c++ - RegCreateKeyEX 返回 ERROR_INVALID_FUNCTION

c++ - 来自 EPEL 的 Amazon Linux 上的 Clang 无法找到 C++ header 或库

C编程: get the size of a two-dimensional pointer to arrays

c - 从文件中读取一行并将其存储在 C 中的字符串数组中

c - 为什么每次运行程序时 argc 的地址都不同?