如何在 Linux 中找到 C 编程语言的头文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13079650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:37:42  来源:igfitidea点击:

How can I find the header files of the C programming language in Linux?

clinuxgccheader-files

提问by Yishu Fang

When I write C programs in Linux, and then compile them using gcc, I am always curious about where those header files are. For example, where stdio.his. More generally, where is stdbool.h?

当我在 Linux 中编写 C 程序,然后使用 gcc 编译它们时,我总是很好奇那些头文件在哪里。例如,在哪里stdio.h。更一般地说,在哪里stdbool.h

What I want to know is not only where it is, but also how to get those places, for example, using shell command or using the C programming language.

我想知道的不仅是它在哪里,还有如何得到那些地方,例如使用shell命令或使用C编程语言。

采纳答案by zwol

gcc -H ...will print the full path of every include file as a side-effect of regular compilation. Use -fsyntax-onlyin addition to get it not to create any output (it will still tell you if your program has errors). Example (Linux, gcc-4.7):

gcc -H ...将打印每个包含文件的完整路径作为常规编译的副作用。使用-fsyntax-only除了让它不创建任何输出(它仍然会告诉你你的程序是否有错误)。示例(Linux,gcc-4.7):

$ cat > test.c
#include <stdbool.h>
#include <stdio.h>
^D
$ gcc -H -fsyntax-only test.c
. /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stdbool.h
. /usr/include/stdio.h
.. /usr/include/features.h
... /usr/include/x86_64-linux-gnu/bits/predefs.h
... /usr/include/x86_64-linux-gnu/sys/cdefs.h
.... /usr/include/x86_64-linux-gnu/bits/wordsize.h
... /usr/include/x86_64-linux-gnu/gnu/stubs.h
.... /usr/include/x86_64-linux-gnu/bits/wordsize.h
.... /usr/include/x86_64-linux-gnu/gnu/stubs-64.h
.. /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stddef.h
.. /usr/include/x86_64-linux-gnu/bits/types.h
... /usr/include/x86_64-linux-gnu/bits/wordsize.h
... /usr/include/x86_64-linux-gnu/bits/typesizes.h
.. /usr/include/libio.h
... /usr/include/_G_config.h
.... /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stddef.h
.... /usr/include/wchar.h
... /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stdarg.h
.. /usr/include/x86_64-linux-gnu/bits/stdio_lim.h
.. /usr/include/x86_64-linux-gnu/bits/sys_errlist.h

The dots at the beginning of each line count how deeply nested the #includeis.

每行开头的点计算嵌套的深度#include

回答by Summer_More_More_Tea

Use gcc -vand you can check the include path. Usually, the include files are in /usr/includeor /usr/local/includedepending on the library installation.

使用gcc -v,您可以检查包含路径。通常,包含文件位于/usr/include/usr/local/include取决于库安装。

回答by Marvo

One approach, if you know the name of the include file, would be to use find:

如果您知道包含文件的名称,一种方法是使用 find:

cd /
find . -name "stdio.h"
find . -name "std*.h"

That'll take a while as it goes through every directory.

这将需要一段时间,因为它会遍历每个目录。

回答by Xymostech

Most standard headers are stored in /usr/include. It looks like stdbool.his stored somewhere else, and depends on which compiler you are using. For example, g++ stores it in /usr/include/c++/4.7.2/tr1/stdbool.hwhereas clang stores it at /usr/lib/clang/3.1/include/stdbool.h.

大多数标准头文件都存储在/usr/include. 它看起来像stdbool.h存储在其他地方,取决于您使用的编译器。例如,g++ 将它存储在,/usr/include/c++/4.7.2/tr1/stdbool.h而 clang 将它存储在/usr/lib/clang/3.1/include/stdbool.h.

回答by guz

locate stdio.h

or

或者

mlocate stdio.h

but locaterelies on a database, if you have never updated it

locate依赖于数据库,如果您从未更新过它

sudo updatedb

you can also enquire gccto know what are the default directories that are scanned by gccitself:

您还可以查询gcc以了解自己扫描的默认目录是什么gcc

gcc -print-search-dirs

回答by Josmar

I think the generic path is:

我认为通用路径是:

/usr/lib/gcc/$(ls /usr/lib/gcc/)/$(gcc -v 2>&1 | tail -1 | awk '{print $3}')/include/stdbool.h

/usr/lib/gcc/$(ls /usr/lib/gcc/)/$(gcc -v 2>&1 | tail -1 | awk '{print $3}')/include/stdbool.h

回答by aschepler

If you use gcc, you can check a specific file with something like:

如果您使用 gcc,您可以使用以下内容检查特定文件:

echo '#include <stdbool.h>' | cpp -H -o /dev/null 2>&1 | head -n1

-Hasks the preprocessor to print all included files recursively. head -n1takes just the first line of output from that, to ignore any files included by the named header (though stdbool.h in particular probably doesn't).

-H要求预处理器递归打印所有包含的文件。 head -n1只需要从中输出的第一行,忽略命名头文件中包含的任何文件(尽管特别是 stdbool.h 可能不会)。

On my computer, for example, the above outputs:

例如,在我的电脑上,上面的输出:

. /usr/lib/gcc/x86_64-linux-gnu/4.6/include/stdbool.h

回答by Sankar Mani

During the preprocessing all preprocessor directives will be replaced with the actuals. Like macro expansion, code comment removal, including the header file source code etc...

在预处理期间,所有预处理器指令都将替换为实际值。像宏扩展,代码注释去除,包括头文件源代码等...

we can check it by using the 'CPP' - C PreProcessor command.

我们可以使用'CPP' - C PreProcessor 命令来检查它。

For example in the command line,

例如在命令行中,

cpp Filename.c

cpp 文件名.c

It will display the preprocessed output.

它将显示预处理的输出。

回答by Jordan

When I was looking for (on Fedora 25) I used "whereis stdio.h" For me, It was in /usr/include/stdio.h, /usr/shar/man/man3/stdio,3.gx. But when you are looking for the file, use whereis or locate

当我在寻找(在 Fedora 25 上)时,我使用了“whereis stdio.h”对我来说,它在 /usr/include/stdio.h、/usr/shar/man/man3/stdio,3.gx 中。但是在查找文件时,请使用 whereis 或 locate

回答by Alvin Z

Use vim to open your source file and put the curses on stdio.h and in normal mode, command 'gf' will let vim open the stdio.h file for you.

使用 vim 打开你的源文件并将诅咒放在 stdio.h 上,在正常模式下,命令“ gf”会让 vim 为你打开 stdio.h 文件。

'Ctr + g' will let vim display the absolute path of stdio.h

' Ctr + g' 会让 vim 显示 stdio.h 的绝对路径