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.h
is. More generally, where is stdbool.h
?
当我在Linux中编写C程序,然后使用gcc编译它们时,我总是很好奇这些头文件在哪里。例如,它的地方。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编程语言。
9 个解决方案
#1
47
gcc -H ...
will print the full path of every include file as a side-effect of regular compilation. Use -fsyntax-only
in 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……将打印所有包含文件的完整路径作为常规编译的副作用。只使用- fsyn,只在不创建任何输出的情况下(它仍然会告诉您程序是否有错误)。示例(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 #include
is.
每行开头的点计算#include嵌套的深度。
#2
22
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
-H
asks the preprocessor to print all included files recursively. head -n1
takes 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
#3
19
locate stdio.h
or
或
mlocate stdio.h
but locate
relies on a database, if you have never updated it
但是定位依赖于一个数据库,如果你从未更新过它。
sudo updatedb
you can also enquire gcc
to know what are the default directories that are scanned by gcc
itself:
您也可以查询gcc,了解gcc本身扫描的默认目录是什么:
gcc -print-search-dirs
#4
4
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预处理器命令来检查它。
For example in the command line,
例如在命令行中,
cpp Filename.c
cpp Filename.c
It will display the preprocessed output.
它将显示预处理后的输出。
#5
2
One approach, if you know the name of the include file, would be to use find:
如果您知道include文件的名称,一种方法是使用find:
cd /
find . -name "stdio.h"
find . -name "std*.h"
That'll take a while as it goes through every directory.
这需要一段时间,因为它会遍历每个目录。
#6
1
Use gcc -v
and you can check the include path. Usually, the include files are in /usr/include
or /usr/local/include
depending on the library installation.
使用gcc -v,您可以检查包含路径。通常,include文件位于/usr/include或/usr/local/include取决于库的安装。
#7
1
Most standard headers are stored in /usr/include
. It looks like stdbool.h
is 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.h
whereas clang stores it at /usr/lib/clang/3.1/include/stdbool.h
.
大多数标准头文件存储在/usr/ includein中。它看起来像stdbool。h存储在其他地方,这取决于您使用哪个编译器。例如,g+将它存储在/usr/include/c+ /4.7.2/tr1/stdbool中。而clang将其存储在/usr/lib/clang/3.1 include/stdbool.h
#8
1
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 | 1 | awk {打印3美元})/ include / stdbool.h
#9
0
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/stdio3. h,/usr/shar/man/man3/stdio,gx。但是,当您在查找文件时,请使用where或locate
#1
47
gcc -H ...
will print the full path of every include file as a side-effect of regular compilation. Use -fsyntax-only
in 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……将打印所有包含文件的完整路径作为常规编译的副作用。只使用- fsyn,只在不创建任何输出的情况下(它仍然会告诉您程序是否有错误)。示例(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 #include
is.
每行开头的点计算#include嵌套的深度。
#2
22
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
-H
asks the preprocessor to print all included files recursively. head -n1
takes 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
#3
19
locate stdio.h
or
或
mlocate stdio.h
but locate
relies on a database, if you have never updated it
但是定位依赖于一个数据库,如果你从未更新过它。
sudo updatedb
you can also enquire gcc
to know what are the default directories that are scanned by gcc
itself:
您也可以查询gcc,了解gcc本身扫描的默认目录是什么:
gcc -print-search-dirs
#4
4
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预处理器命令来检查它。
For example in the command line,
例如在命令行中,
cpp Filename.c
cpp Filename.c
It will display the preprocessed output.
它将显示预处理后的输出。
#5
2
One approach, if you know the name of the include file, would be to use find:
如果您知道include文件的名称,一种方法是使用find:
cd /
find . -name "stdio.h"
find . -name "std*.h"
That'll take a while as it goes through every directory.
这需要一段时间,因为它会遍历每个目录。
#6
1
Use gcc -v
and you can check the include path. Usually, the include files are in /usr/include
or /usr/local/include
depending on the library installation.
使用gcc -v,您可以检查包含路径。通常,include文件位于/usr/include或/usr/local/include取决于库的安装。
#7
1
Most standard headers are stored in /usr/include
. It looks like stdbool.h
is 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.h
whereas clang stores it at /usr/lib/clang/3.1/include/stdbool.h
.
大多数标准头文件存储在/usr/ includein中。它看起来像stdbool。h存储在其他地方,这取决于您使用哪个编译器。例如,g+将它存储在/usr/include/c+ /4.7.2/tr1/stdbool中。而clang将其存储在/usr/lib/clang/3.1 include/stdbool.h
#8
1
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 | 1 | awk {打印3美元})/ include / stdbool.h
#9
0
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/stdio3. h,/usr/shar/man/man3/stdio,gx。但是,当您在查找文件时,请使用where或locate