# include”窗口。“它是怎么工作的?”

时间:2022-01-17 05:03:19

When I include "windows.h" in a c program in the following manner

当我包括“窗口。h“在c程序中以下列方式进行。

#include "windows.h"

it compiles fine. According to my knowledge if we include a header file with "" the compiler would search for header files in the current directory. Then how does my program compile without a header file as such?

它运行良好。根据我的知识,如果我们包含一个头文件和“”,编译器将在当前目录中搜索头文件。那么,我的程序如何在没有头文件的情况下编译呢?

1 个解决方案

#1


4  

The treatment of the quoted form of #include is implementation-defined. The standard (ISO/IEC 9899:201x, 6.10.2.3) says, with my emphasis:

所引用的#include的处理是由实现定义的。标准(ISO/ iec9899:201x, 6.10.2.3)强调:

A preprocessing directive of the form

表单的预处理指令。

# include "q-char-sequence" new-line

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read

导致由“分隔符”之间的指定序列标识的源文件的全部内容替换该指令。以实现定义的方式搜索命名的源文件。如果不支持这个搜索,或者如果搜索失败,则该指令将被重新处理,就像读取它一样。

# include <h-char-sequence> new-line

with the identical contained sequence (including > characters, if any) from the original directive.

使用相同的包含序列(包括>字符,如果有的话)来自原始指令。

So to answer the question you need to refer to the documentation of your compiler.

因此,要回答这个问题,您需要参考编译器的文档。

For the sake of argument and illustration, let's assume that you use the MS compiler, since you did not specify which compiler you use. The documentation for the MS compiler says, for the quoted form of #include:

为了论证和说明,让我们假设您使用MS编译器,因为您没有指定使用哪个编译器。MS编译器的文档说明了#include:

The preprocessor searches for include files in the following order:

预处理器搜索包含以下顺序的文件:

  1. In the same directory as the file that contains the #include statement.
  2. 在与包含#include语句的文件相同的目录中。
  3. In the directories of any previously opened include files in the reverse order in which they were opened. The search starts from the directory of the include file that was opened last and continues through the directory of the include file that was opened first.
  4. 在任何以前打开的目录中,都包含打开它们的相反顺序的文件。搜索从已打开的include文件的目录开始,并继续通过包含先打开的include文件的目录。
  5. Along the path specified by each /I compiler option.
  6. 沿着每个/I编译器选项指定的路径。
  7. Along the paths specified by the INCLUDE environment variable.
  8. 沿着INCLUDE环境变量指定的路径。

If you are using a different compiler then you will need to refer to the documentation for your compiler.

如果您使用的是不同的编译器,那么您需要参考编译器的文档。

#1


4  

The treatment of the quoted form of #include is implementation-defined. The standard (ISO/IEC 9899:201x, 6.10.2.3) says, with my emphasis:

所引用的#include的处理是由实现定义的。标准(ISO/ iec9899:201x, 6.10.2.3)强调:

A preprocessing directive of the form

表单的预处理指令。

# include "q-char-sequence" new-line

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read

导致由“分隔符”之间的指定序列标识的源文件的全部内容替换该指令。以实现定义的方式搜索命名的源文件。如果不支持这个搜索,或者如果搜索失败,则该指令将被重新处理,就像读取它一样。

# include <h-char-sequence> new-line

with the identical contained sequence (including > characters, if any) from the original directive.

使用相同的包含序列(包括>字符,如果有的话)来自原始指令。

So to answer the question you need to refer to the documentation of your compiler.

因此,要回答这个问题,您需要参考编译器的文档。

For the sake of argument and illustration, let's assume that you use the MS compiler, since you did not specify which compiler you use. The documentation for the MS compiler says, for the quoted form of #include:

为了论证和说明,让我们假设您使用MS编译器,因为您没有指定使用哪个编译器。MS编译器的文档说明了#include:

The preprocessor searches for include files in the following order:

预处理器搜索包含以下顺序的文件:

  1. In the same directory as the file that contains the #include statement.
  2. 在与包含#include语句的文件相同的目录中。
  3. In the directories of any previously opened include files in the reverse order in which they were opened. The search starts from the directory of the include file that was opened last and continues through the directory of the include file that was opened first.
  4. 在任何以前打开的目录中,都包含打开它们的相反顺序的文件。搜索从已打开的include文件的目录开始,并继续通过包含先打开的include文件的目录。
  5. Along the path specified by each /I compiler option.
  6. 沿着每个/I编译器选项指定的路径。
  7. Along the paths specified by the INCLUDE environment variable.
  8. 沿着INCLUDE环境变量指定的路径。

If you are using a different compiler then you will need to refer to the documentation for your compiler.

如果您使用的是不同的编译器,那么您需要参考编译器的文档。