包含的路径解析

发布于 2024-10-17 07:19:56 字数 170 浏览 1 评论 0原文

是否有明确的方法来查看给定的 #include 解析到的位置?我的代码中有一个 #include ,但我不知道正在使用哪个 unistd.h

Is there a definitive way to see where a given #include <example.h> resolves to? I have a #include <linux/unistd.h> in my code but I don't know which unistd.h is being used.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘味果▽酱 2024-10-24 07:19:56

如果您使用 -E 命令行选项来获取预处理器输出,它将告诉您包含的每个头文件的完整路径,包括其他头文件包含的文件。例如:

$ cat test.c
#include <unistd.h>
$ gcc -E test.c
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"
# 1 "/usr/include/unistd.h" 1 3 4
# 71 "/usr/include/unistd.h" 3 4
# 1 "/usr/include/_types.h" 1 3 4
# 27 "/usr/include/_types.h" 3 4
# 1 "/usr/include/sys/_types.h" 1 3 4
# 32 "/usr/include/sys/_types.h" 3 4
# 1 "/usr/include/sys/cdefs.h" 1 3 4
# 33 "/usr/include/sys/_types.h" 2 3 4
# 1 "/usr/include/machine/_types.h" 1 3 4
# 34 "/usr/include/machine/_types.h" 3 4
# 1 "/usr/include/i386/_types.h" 1 3 4
# 37 "/usr/include/i386/_types.h" 3 4
typedef signed char __int8_t;

(lots more output)

因此,在本例中,使用的标头是 /usr/include/unistd.h

If you use the -E command line option to get the preprocessor output, it will tell you the full path to every header file included, including those included by other headers. For example:

$ cat test.c
#include <unistd.h>
$ gcc -E test.c
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"
# 1 "/usr/include/unistd.h" 1 3 4
# 71 "/usr/include/unistd.h" 3 4
# 1 "/usr/include/_types.h" 1 3 4
# 27 "/usr/include/_types.h" 3 4
# 1 "/usr/include/sys/_types.h" 1 3 4
# 32 "/usr/include/sys/_types.h" 3 4
# 1 "/usr/include/sys/cdefs.h" 1 3 4
# 33 "/usr/include/sys/_types.h" 2 3 4
# 1 "/usr/include/machine/_types.h" 1 3 4
# 34 "/usr/include/machine/_types.h" 3 4
# 1 "/usr/include/i386/_types.h" 1 3 4
# 37 "/usr/include/i386/_types.h" 3 4
typedef signed char __int8_t;

(lots more output)

So in this case, the header that's being used is /usr/include/unistd.h.

疾风者 2024-10-24 07:19:56

GCC 文档

在普通的 Unix 系统上,如果你不这样做
否则指示它,它会看起来
对于使用 #include 请求的标头
在:

<前><代码> /usr/local/include
libdir/gcc/目标/版本/包括
/usr/目标/包括
/usr/包括

上面的目标是规范的
配置的系统名称 GCC
编译代码;经常但不
始终与规范名称相同
它运行的系统的。版本是
正在使用的 GCC 版本。

您可以使用以下命令添加到此列表
-Idir 命令行选项。搜索所有以-I命名的目录,
按从左到右的顺序,在
默认目录。唯一的
例外是当 dir 已经存在时
默认搜索。在这种情况下,
选项被忽略并且搜索顺序
对于系统目录仍然存在
不变。

因此,除非您添加 -I (大写字母,而不是 ell)开关,否则包含的版本是在包含它的第一个目录中找到的版本。

From the GCC documentation,

On a normal Unix system, if you do not
instruct it otherwise, it will look
for headers requested with #include
in:

 /usr/local/include
 libdir/gcc/target/version/include
 /usr/target/include
 /usr/include

In the above, target is the canonical
name of the system GCC was configured
to compile code for; often but not
always the same as the canonical name
of the system it runs on. version is
the version of GCC in use.

You can add to this list with the
-Idir command line option. All the directories named by -I are searched,
in left-to-right order, before the
default directories. The only
exception is when dir is already
searched by default. In this case, the
option is ignored and the search order
for system directories remains
unchanged.

So unless you're adding the -I (capital eye, not ell) switch, the version included is the version found in the first of those directories that holds it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文