链接库时默认目录中的gcc或g ++路径搜索顺序 [英] gcc or g++ path search order on default directories when linking libraries

查看:481
本文介绍了链接库时默认目录中的gcc或g ++路径搜索顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过这篇文章,并了解到:


  1. 在命令行中使用 -L 选项指定的目录在默认目录之前搜索。

  2. -L 中指定的目录将按照它们在命令行中指定的顺序进行搜索。

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

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

  $ gcc -Xlinker --verbose 2> / dev / null | grep搜索| sed's / SEARCH_DIR(= \?\([^] \ + \)); * / \\\\\'/''  

(从复制的命令文章



之前打印出 / usr / local / lib / usr / lib 在我的机器上(Ubuntu 16.04,64位,gcc 5.4.0),在这种情况下,将 / usr / local / lib / usr / lib

解决方案

之前搜索 binutils ld手册部分 3.4.2处理文件的命令


SEARCH_DIR(路径)



SEARCH_DIR命令将路径添加到ld查找归档库的路径列表中。使用SEARCH_DIR路径)就像使用`-L路径一样'在命令行上(请参阅命令行选项)。如果两者都使用,则链接器将搜索两个路径。首先搜索使用命令行选项指定的路径。


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



例如,在我的 mingw 安装中,默认的链接器脚本以这种方式启动:

 / *默认链接描述文件,用于普通可执行文件* / 
/ *版权所有(C)2014-2017 Free Software Foundation,Inc.
复制和分发此脚本,无论是否进行修改,
都可以在没有版税的任何媒介中获得,只要版权
通知并且保留此通知。 * /
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 中的库,但不会提供<$ c提供的库$ c> mingw 本身。


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?

解决方案

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.

这篇关于链接库时默认目录中的gcc或g ++路径搜索顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆