如何在Linux中找到C编程语言的头文件?
当我用Linux编写C程序,然后使用gcc编译它们时,我总是对那些头文件在哪里感到好奇。 例如,其中stdio.h是。 更笼统地说,stdbool.h在哪里?
我不仅想知道它在哪里,而且还想知道如何获得这些地方,例如,使用shell命令或使用C编程语言。
10个解决方案
62 votes
#include将打印每个包含文件的完整路径,作为常规编译的副作用。 除了使用-fsyntax-only之外,不要使用它来创建任何输出(它仍会告诉您程序是否有错误)。 示例(Linux,gcc-4.7):
$ cat > test.c
#include
#include
^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
每行开头的点表示#include嵌套的深度。
zwol answered 2020-01-25T01:29:10Z
27 votes
如果使用gcc,则可以使用以下命令检查特定文件:
echo '#include ' | cpp -H -o /dev/null 2>&1 | head -n1
-H要求预处理器以递归方式打印所有包含的文件。 head -n1仅从输出的第一行开始,就忽略了命名标头包含的任何文件(尽管stdbool.h特别可能没有)。
例如,在我的计算机上,上述输出:
. /usr/lib/gcc/x86_64-linux-gnu/4.6/include/stdbool.h
aschepler answered 2020-01-25T01:29:39Z
22 votes
locate stdio.h
要么
mlocate stdio.h
但是gcc依赖数据库,如果您从未更新过的话
sudo updatedb
您还可以查询gcc来了解什么是gcc本身扫描的默认目录:
gcc -print-search-dirs
guz answered 2020-01-25T01:30:08Z
4 votes
在预处理期间,所有预处理器指令都将替换为实际值。 像宏扩展一样,删除代码注释,包括头文件的源代码等。
我们可以使用“ CPP”-C PreProcessor命令进行检查。
例如在命令行中
cpp文件名.c
它将显示预处理的输出。
Sankar Mani answered 2020-01-25T01:30:45Z
2 votes
如果您知道包含文件的名称,则一种方法是使用find:
cd /
find . -name "stdio.h"
find . -name "std*.h"
遍历每个目录将花费一些时间。
Marvo answered 2020-01-25T01:31:09Z
1 votes
使用gcc -v,您可以检查包含路径。通常,包含文件位于/usr/include或/usr/local/include中,具体取决于库的安装。
Summer_More_More_Tea answered 2020-01-25T01:31:30Z
1 votes
大多数标准标头存储在/usr/include中。看起来stdbool.h存储在其他地方,并且取决于您使用的编译器。 例如,g ++将其存储在/usr/include/c++/4.7.2/tr1/stdbool.h中,而clang将其存储在/usr/lib/clang/3.1/include/stdbool.h中。
Xymostech answered 2020-01-25T01:31:50Z
1 votes
我认为一般路径是:
/ usr / lib / gcc / $(ls / usr / lib / gcc /)/ $(gcc -v 2>&1 | tail -1 | awk'{print $ 3}')/ include / stdbool.h
Josmar answered 2020-01-25T01:32:14Z
0 votes
当我寻找(在Fedora 25上)时,我使用了“ stdio.h在哪里”。对我来说,它在/usr/include/stdio.h、/usr/share/man/man3/stdio,3.gx中。 但是当您查找文件时,请使用whereis或定位
Jordan answered 2020-01-25T01:32:34Z
-1 votes
使用vim打开源文件,然后将curses放在stdio.h上。在正常模式下,命令'gf'将使vim为您打开stdio.h文件。
'Ctr + g'将让vim显示stdio.h的绝对路径
Alvin Z answered 2020-01-25T01:32:59Z