#include的用法

sf417588322 2008-09-24 10:12:41
#include包含文件有两种方式,一:<> 另一种是:“”,在执行时,编译器是怎样查找文件路径的?请详细点。谢谢。
...全文
4421 56 打赏 收藏 转发到动态 举报
写回复
用AI写文章
56 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-02-08
  • 打赏
  • 举报
回复
[Quote=引用 51 楼 zhao4zhong1 的回复:]
The #include Directive
The #include directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive……
[/Quote]
正解!(^_^)
Wdayear 2012-02-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 devil_zuiai 的回复:]

对于#include <filename.h> ,编译器从标准库路径开始搜索
对于#include “filename.h” ,编译器从用户的工作路径开始搜索
一般自己写的.h在工作目录,所以就用引号,节省搜索时间
[/Quote]正解。
JayDa214 2012-02-08
  • 打赏
  • 举报
回复
学习了.....
lhappyb 2012-02-08
  • 打赏
  • 举报
回复
只能UP表达感情
pp25210 2012-02-08
  • 打赏
  • 举报
回复
学习了
wenxingone 2011-12-27
  • 打赏
  • 举报
回复
简单点:“”是从本地目录里找头文件,<>是从系统目录中找头文件。
upsate 2011-12-27
  • 打赏
  • 举报
回复
醍醐灌顶!!!
yuefy520 2011-12-27
  • 打赏
  • 举报
回复
真的是经典!!!
赵4老师 2011-12-27
  • 打赏
  • 举报
回复
The #include Directive
The #include directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears. You can organize constant and macro definitions into include files and then use #include directives to add these definitions to any source file. Include files are also useful for incorporating declarations of external variables and complex data types. You only need to define and name the types once in an include file created for that purpose.

Syntax

#include "path-spec"

#include <path-spec>

The path-spec is a filename optionally preceded by a directory specification. The filename must name an existing file. The syntax of the path-spec depends on the operating system on which the program is compiled.

Both syntax forms cause replacement of that directive by the entire contents of the specified include file. The difference between the two forms is the order in which the preprocessor searches for header files when the path is incompletely specified.

Syntax Form Action
Quoted form This form instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of whatever files that include (#include) that file. The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable.
Angle-bracket form This form instructs the preprocessor to search for include files first along the path specified by the /I compiler option, then along the path specified by the INCLUDE environment variable.


The preprocessor stops searching as soon as it finds a file with the given name. If you specify a complete, unambiguous path specification for the include file between two sets of double quotation marks (" "), the preprocessor searches only that path specification and ignores the standard directories.

If the filename enclosed in double quotation marks is an incomplete path specification, the preprocessor first searches the “parent” file’s directory. A parent file is the file containing the #include directive. For example, if you include a file named file2 within a file named file1, file1 is the parent file.

Include files can be “nested”; that is, an #include directive can appear in a file named by another #include directive. For example, file2, above, could include file3. In this case, file1 would still be the parent of file2 but would be the “grandparent” of file3.

When include files are nested, directory searching begins with the directories of the parent file and then proceeds through the directories of any grandparent files. Thus, searching begins relative to the directory containing the source currently being processed. If the file is not found, the search moves to directories specified by the /I compiler option. Finally, the directories specified by the INCLUDE environment variable are searched.

The following example shows file inclusion using angle brackets:

#include <stdio.h>

This example adds the contents of the file named STDIO.H to the source program. The angle brackets cause the preprocessor to search the directories specified by the INCLUDE environment variable for STDIO.H, after searching directories specified by the /I compiler option.

The following example shows file inclusion using the quoted form:

#include "defs.h"

This example adds the contents of the file specified by DEFS.H to the source program. The double quotation marks mean that the preprocessor searches the directory containing the parent source file first.

Nesting of include files can continue up to 10 levels. Once the nested #include is processed, the preprocessor continues to insert the enclosing include file into the original source file.

Microsoft Specific

To locate includable source files, the preprocessor first searches the directories specified by the /I compiler option. If the /I option is not present or fails, the preprocessor uses the INCLUDE environment variable to find any include files within angle brackets. The INCLUDE environment variable and /I compiler option can contain multiple paths separated by semicolons (;). If more than one directory appears as part of the /I option or within the INCLUDE environment variable, the preprocessor searches them in the order in which they appear.

For example, the command

CL /ID:\MSVC\INCLUDE MYPROG.C

causes the preprocessor to search the directory D:\MSVC\INCLUDE for include files such as STDIO.H. The commands

SET INCLUDE=D:\MSVC\INCLUDE
CL MYPROG.C

have the same effect. If both sets of searches fail, a fatal compiler error is generated.

If the filename is fully specified for an include file with a path that includes a colon (for example, F:\MSVC\SPECIAL\INCL\TEST.H), the preprocessor follows the path.

For include files specified as #include "path-spec", directory searching begins with the directory of the parent file and then proceeds through the directories of any grandparent files. Thus, searching begins relative to the directory containing the source file containing the #include directive being processed. If there is no grandparent file and the file has not been found, the search continues as if the filename were enclosed in angle brackets.

END Microsoft Specific
yanqingluo_nanjing 2011-11-25
  • 打赏
  • 举报
回复
UP.基础且经典.
yiruirui0507 2010-06-22
  • 打赏
  • 举报
回复
采用"":依次搜索当前目录、包括/I编译开关的路径、包括INCLUDE环境变量的路径
采用<>:依次搜索包括/I编译开关的路径、包括INCLUDE环境变量的路径
bobo364 2010-06-22
  • 打赏
  • 举报
回复
"" 这个是先从自己的当前目录下开始查找,如果没有,再从安装库里查找

<>这个直接从安装库里查找
orient_hwa 2010-06-22
  • 打赏
  • 举报
回复
还有一个大家是否知道,要在你工具中定义清晰除了系统目录外,还有你的用户目录,否则会搜索不到
ykhufuhai 2010-06-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 devil_zuiai 的回复:]
对于#include <filename.h> ,编译器从标准库路径开始搜索
对于#include “filename.h” ,编译器从用户的工作路径开始搜索
一般自己写的.h在工作目录,所以就用引号,节省搜索时间
[/Quote]

up
china_west 2010-06-22
  • 打赏
  • 举报
回复
"" 这个是先从自己的当前目录下开始查找,如果没有,再从安装库里查找

<>这个直接从安装库里查找
jianuMan 2010-06-22
  • 打赏
  • 举报
回复
"" 从程序当前目录开始搜索 然后搜索系统目录
<> 直接从系统目录找起
elpase 2010-06-22
  • 打赏
  • 举报
回复
看了不少人的回复, 都没有回复完全, 不过综合起来就差不多了
时间一粒 2010-06-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hqin6 的回复:]
C/C++ code


使用尖括号的话,编译时会先在系统include目录里搜索,如果找不到才会在源代码所在目录搜索;使用双引号则相反,会先在源代码目录里搜索。这就意味着,当系统里(如/usr/include/里)有一个叫做math.h的头文件,而你的源代码目录里也有一个你自己写的math.h头文件,那么使用尖括号时用的就是系统里的;而使用双引号的话则会使用你自己写的那个。
……
[/Quote]
呃..........
这个学习了!
a6369508 2010-06-21
  • 打赏
  • 举报
回复
学到东西啦
czbever 2008-10-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jillnicky 的回复:]
编译器怎么查找文件路径?
[/Quote]

这个是错的 


#include <>从编译器从标准库INCLUDE文件开始搜索
#include “ ”从当前工作路径开始搜索,未查到再在标准库查找

这个才是对的


加载更多回复(36)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧