【问题标题】:Determining where header file is found确定头文件的位置
【发布时间】:2018-08-16 14:17:12
【问题描述】:

当我使用gcc -v hello.c* 编译时,输出显示#include 的搜索路径:

$ gcc -v hello.c
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
...
clang -cc1 version 9.1.0 (clang-902.0.39.2) default target x86_64-apple-darwin17.7.0
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/lib/clang/9.1.0/include
 /Library/Developer/CommandLineTools/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.

有没有办法确定使用了这些位置中的哪一个?例如,假设hello.c 包含#include &lt;stdio.h&gt;。我可以手动看到stdio.h在多个位置的版本,大概可以使用不同的函数构造:

$ find /usr/local/include -name "stdio.h"
/usr/local/include/c++/5.5.0/tr1/stdio.h

$ find /Library/Developer/CommandLineTools/usr/include -name "stdio.h"
/Library/Developer/CommandLineTools/usr/include/c++/v1/stdio.h

$ find /usr/include -name "stdio.h"
/usr/include/c++/4.2.1/tr1/stdio.h
/usr/include/sys/stdio.h
/usr/include/stdio.h

在 Python 中,这看起来像:

>>> import math
>>> math.__file__
'/Users/brad/miniconda3/lib/python3.6/lib-dynload/math.cpython-36m-darwin.so'

*我在 Macbook 上,所以 gcc 实际上似乎路由到 clang,尽管它似乎是真正的可执行文件而不是符号链接。

【问题讨论】:

  • 使用-H 选项获取包含的标头列表。
  • @JonathanLeffler 您是否知道列出的路径是否按顺序搜索? (并且递归?)
  • 搜索顺序很复杂,部分取决于标题的位置。但是,当没有其他东西可以改变顺序时,将按顺序搜索列出的路径。请参阅 POSIX c99 命令和 -I 选项的文档;对于 GCC,请参阅 Options for directory search 和(不太重要的)Options controlling the preprocessor

标签: c gcc clang


【解决方案1】:

来自gcc man页面:

-H

打印使用的每个头文件的名称,以及其他正常活动。每个名称都缩进以显示它在#include 堆栈中的深度。预编译的头文件也会被打印出来,即使被发现是无效的;一个无效的预编译头文件用 ...x 打印,一个有效的用 ...! .

【讨论】:

  • 请注意,OP 的gcc 版本实际上根本不是gcc,而是clang(必须是Mac 用户)。
猜你喜欢
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 2016-11-15
  • 2011-11-30
  • 2022-10-17
  • 2011-11-25
相关资源
最近更新 更多