为什么"/usr/include"不在 GCC 默认搜索路径中

why `/usr/include` is not in gcc default search path

本文关键字:quot 搜索 路径 默认 include usr 为什么 不在 GCC      更新时间:2023-10-16

众所周知,gcc 在编译目标时会搜索一些默认的 dir, 我使用gcc -print-search-dirscommnd 并得到这些东西:

install: /usr/lib/gcc/x86_64-redhat-linux/4.8.5/
programs: =/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../x86_64-redhat-linux/bin/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../x86_64-redhat-linux/bin/
libraries: =/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../x86_64-redhat-linux/lib/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../x86_64-redhat-linux/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/:/lib/x86_64-redhat-linux/4.8.5/:/lib/../lib64/:/usr/lib/x86_64-redhat-linux/4.8.5/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../x86_64-redhat-linux/lib/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../:/lib/:/usr/lib/

我对此有两个问题:

  1. /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../x86_64-redhat-linux/bin/是什么意思,为什么要这样写?

  2. 为什么/usr/include不存在?gcc 不需要知道头文件在哪里吗?

至于问题1,我在gcc邮件列表中找到了这个:

请注意,a/b/c/../../../x 仅在 a/b/c 存在时才存在。

因此,您的 gcc 只会在/usr/x86_64-redhat-linux/bin/中搜索/usr/lib/gcc/x86_64-redhat-linux/4.8.5/如果存在。

请注意,我在 arch 上有类似的搜索目录,而/usr/x86_64-redhat-linux/等效的搜索目录不存在,所以我不确定为什么它们甚至被包括在内,但 Linux 有一个臭名昭著的非标准化目录结构,所以也许它对其他发行版很重要。

关于 2,从手册页:

-打印搜索目录

Print the name of the configured installation directory and a list of program and library directories gcc searches---and don't do anything else.
This is useful when gcc prints the error message installation problem, cannot exec cpp0: No such file or directory.  To resolve this you either need to put cpp0 and the other
compiler components where gcc expects to find them, or you can set the environment variable GCC_EXEC_PREFIX to the directory where you installed them.  Don't forget the
trailing /.

这似乎打印的不是库路径,而是 gcc 期望找到它调用的组件来完成工作的路径。

要查找预处理器搜索的路径,请键入

`gcc -print-prog-name=cc1plus` -v

`gcc -print-prog-name=cc1` -v

对于 C 预处理器。