找到 C 头文件“stdio.h”

找到 C 头文件“stdio.h”

我编写了第一个 C 程序,它是通用的“hello world”程序。谁能告诉我stdio.h在我的系统中哪里可以找到文件?这对我理解语言有很大帮助。

答案1

在您最初的问题表明您正在使用的 Ubuntu 中,“根”stdio.h文件位于/usr/include.其他系统可能会将其放在其他地方,并且在所有情况下都可能会stdio.h引入其他文件。

您可能会发现阅读它更有帮助POSIX 规范stdio.h,然后看看您的系统如何实现它。在 Linux 上,man 3 stdio还提供了有用的摘要;其他系统也有类似的文档。

答案2

它取决于系统(请注意POSIX没有指定头文件的位置,C 标准也没有指定)。按照惯例,它可以在 中找到/usr/include。然而,在某些系统上(甚至是那些拥有目录/usr/include),可能在那里找不到。

例如,最近(过去几年)的 macOS SDK 将其放在 SDK 下的目录中(就像 Visual Studio...)。在我的一台机器上查找stdio.h给出了这个列表,例如:

/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/DriverKit19.0.sdk/System/DriverKit/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/stdio.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/stdio.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/stdio.h
/Library/Developer/CommandLineTools/usr/include/c++/v1/stdio.h
/opt/local/include/gcc/c++/tr1/stdio.h
/opt/local/include/gcc8/c++/tr1/stdio.h
/opt/local/include/gcc9/c++/tr1/stdio.h
/opt/local/include/isc/stdio.h
/opt/local/lib/gcc8/gcc/x86_64-apple-darwin18/8.3.0/include/ssp/stdio.h
/opt/local/lib/gcc8/gcc/x86_64-apple-darwin18/8.3.0/include-fixed/stdio.h
/opt/local/lib/gcc9/gcc/x86_64-apple-darwin18/9.2.0/include/ssp/stdio.h
/opt/local/lib/gcc9/gcc/x86_64-apple-darwin18/9.2.0/include-fixed/stdio.h
/opt/local/libexec/llvm-9.0/include/c++/v1/stdio.h
/opt/local/libexec/llvm-9.0/lib/c++/v1/stdio.h

下面的文件/opt适用于 MacPorts(第三方)。

即使文件是 /usr/include,其大部分内容(实现细节)可能位于另一个文件中。内部详细信息可能位于/usr/include(Solaris) 的子目录中,也可能不在(通常不是)中。同样,macOS 是一个例子,但还有其他例子。如果没有涉及到的系统的详细信息,没有人能够给出详细的答案。

通常(同样,并非总是),您的编译器及其关联的 C 预处理器可以帮助找到所使用的实际文件。您必须查阅相应的文档,但这对我在 macOS 上的 hello-world 程序有效:

/usr/bin/clang -E hello.c >hello.log

(对于答案来说有点太长了)这样开始:

# 1 “你好.c”
#1“”1
#1“”3
第366章 3
#1“”1
#1“”2
# 1 “你好.c” 2
# 1“/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/stdio.h”1 3 4
# 64“/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/stdio.h”3 4
# 1“/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/_stdio.h”1 3 4
# 68“/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/_stdio.h”3 4
# 1“/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/cdefs.h”1 3 4
...

相关内容