为什么 g++ 在 LIBRARY_PATH/../lib64 中查找,这在哪里记录? [英] Why does g++ look in LIBRARY_PATH/../lib64 and where is this documented?

查看:18
本文介绍了为什么 g++ 在 LIBRARY_PATH/../lib64 中查找,这在哪里记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 LIBRARY_PATH 环境变量中有一个自定义目录:/cs/public/lib/pkg/opencv/lib.

但是,当我使用 g++ --print-search-dirs 时,我得到了这个:

库:=/cs/public/lib/pkg/opencv/lib/x86_64-suse-linux/4.6/:/cs/public/lib/pkg/opencv/lib/../lib64/:/usr/lib64/gcc/x86_64-suse-linux/4.6/:/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/lib/x86_64-suse-linux/4.6/:/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/lib/../lib64/:/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../x86_64-suse-linux/4.6/:/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../lib64/:/lib/x86_64-suse-linux/4.6/:/lib/../lib64/:/usr/lib/x86_64-suse-linux/4.6/:/usr/lib/../lib64/:/cs/public/lib/pkg/opencv/lib/:/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/lib/:/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../:/lib/:/usr/lib/

为什么 g++ 会在这些替代方案和一大堆其他系统位置之前查看我在 LIBRARY_PATH 变量中明确指定的内容,并且记录在哪里?p>

如果在 LIBRARY_PATH 和 LIBRARY_PATH/../lib64 等之前搜索系统默认值,我会理解,但是 g++ 会先放置 LIBRARY_PATH/../lib64,然后是系统路径,然后是 LIBRARY_PATH.此订购记录在哪里?

我的 g++ 版本是 g++ (SUSE Linux) 4.6.2

我的操作系统是 openSUSE 12.1 (x86_64)

解决方案

这里问了一个类似的问题:g++ 搜索/lib/../lib/,然后搜索/lib/

这些看起来很吓人的搜索路径至少部分是在它构建的编译器本身时确定的,例如在配置阶段.很明显,它超越了环境变量,因为可以安装多个 GCC 副本,并让每个副本为 gcc --print-search-dirs 提供不同的结果.还要注意 g++ --print-search-dirsgcc --print-search-dirs 给出不同的结果指出 g++ 包装器也在影响搜索路径.除了配置/构建时间差异之外,GCC 肯定知道它自己的可执行文件所在的路径,并将搜索该路径的子目录.在 GCC 文档中可以找到很多这种炼金术:
http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Directory-Options.html#Directory-Options
http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Environment-Variables.html#Environment-Variables

据我所知,在不编译自己的 GCC 副本的情况下,最有力的方法是使用 -L 选项指定自定义库.我这么说的原因是因为 -L 在例如之前被搜索过.LIBRARY_PATH(请参阅上面有关环境变量的链接).为了让它更容易忍受,你可以为 g++ 添加一个别名,包括在你的 .bashrc 文件中的 -L 选项.

如果您想要一个明确的答案,那么下载 GCC 源代码的副本是一种方法.例如,在 gcc.c 中会出现以下高度暗示性的注释:

/* 从 PATHS 构建搜索目录列表.PREFIX 是要添加到列表的字符串.如果 CHECK_DIR_P 为真,我们确保目录存在.如果 DO_MULTI 为真,则首先输出多库路径,然后非多库路径.这主要由 putenv_from_prefixes 使用,所以我们使用 `collect_obstack'.--print-search-dirs 标志也使用它.*/

不过注释后面的功能不是很明显.

My LIBRARY_PATH environment variable has a custom directory in it: /cs/public/lib/pkg/opencv/lib.

But, when I use g++ --print-search-dirs, I get this instead:

libraries: =
/cs/public/lib/pkg/opencv/lib/x86_64-suse-linux/4.6/:
/cs/public/lib/pkg/opencv/lib/../lib64/:
/usr/lib64/gcc/x86_64-suse-linux/4.6/:
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/lib/x86_64-suse-linux/4.6/:
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/lib/../lib64/:
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../x86_64-suse-linux/4.6/:
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../lib64/:
/lib/x86_64-suse-linux/4.6/:
/lib/../lib64/:
/usr/lib/x86_64-suse-linux/4.6/:
/usr/lib/../lib64/:
/cs/public/lib/pkg/opencv/lib/:
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/lib/:
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../:
/lib/:
/usr/lib/

Why does g++ look in these alternatives and a whole bunch of other system locations before what I explicitly specify in the LIBRARY_PATH variable, and where is this documented?

I would understand if system defaults were searched before LIBRARY_PATH and LIBRARY_PATH/../lib64, etc, but g++ puts LIBRARY_PATH/../lib64, then system paths, then LIBRARY_PATH. Where is this ordering documented?

My g++ version is g++ (SUSE Linux) 4.6.2

My OS is openSUSE 12.1 (x86_64)

解决方案

A similar question was asked here: g++ searches /lib/../lib/, then /lib/

These scary-looking search paths are determined at least in part when the compiler itself it built, for example during the configure phase. It's clear that it goes beyond environment variables because it's possible to have multiple copies of GCC installed and have each of them give different results for gcc --print-search-dirs. Also noting that g++ --print-search-dirs and gcc --print-search-dirs give different results points out that the g++ wrapper is also affecting the search path. Besides configure/build time differences, GCC is definitely aware of the path where its own executable is, and will search subdirectories of that path. A lot of this alchemy can be found in the GCC documentation:
http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Directory-Options.html#Directory-Options
http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Environment-Variables.html#Environment-Variables

As far as I know, the most forceful thing that you can without compiling your own copy of GCC is to specify your custom libraries using the -L option. The reason I say this is because -L is searched before e.g. LIBRARY_PATH (see the above link on environment variables). In order to make it more tolerable you could add an alias for g++ including the -L option in your .bashrc file.

If you want a definitive answer then downloading a copy of the GCC source code is one way. For example, in gcc.c the following highly suggestive comment appears:

/* Build a list of search directories from PATHS.
   PREFIX is a string to prepend to the list.
   If CHECK_DIR_P is true we ensure the directory exists.
   If DO_MULTI is true, multilib paths are output first, then
   non-multilib paths.
   This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
   It is also used by the --print-search-dirs flag.  */

However the function that follows the comment is not very obvious.

这篇关于为什么 g++ 在 LIBRARY_PATH/../lib64 中查找,这在哪里记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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