user-0 提问: 求助 怎么包含头文件和include的用法

回复:
user: user-1
content: 引用 6 楼 u013539260 的回复:[quote=引用 2 楼 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 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 谢谢赵老师，很好奇这些资料你在哪里找的呢？你知道的，英文权威资料不好找啊。[/quote] MSDN98

user: user-2
content: 一般的库里的用include<xxx>；自定义的用include "路径/xxx"；自定义的在同一目录下的话可以不用+路径

user: user-0
content: 引用 2 楼 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 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 谢谢赵老师，很好奇这些资料你在哪里找的呢？你知道的，英文权威资料不好找啊。

user: user-0
content: 引用 3 楼 colddown 的回复:如果楼主用的是gcc, 可以用-E选项进行预编译，这样就能看出是include哪些文件了。头文件的搜寻路径一般以-I 或-i参数形式出现在gcc的命令行里，楼主可以看看Makefile。  不知道在哪里看makefie,请问下在哪里看呢？以前的都是.c在一个文件.h的在另一个文件。现在当文件很多的时候，我不知道怎么放头文件。

user: user-0
content: 引用 1 楼 whustyle 的回复:1如果你所说的“几个路径”、“同一路径”是指文件目录的路径，那么自定义头文件是需要放在同一个工程文件夹路径下； 2 只要变量所在的头文件，存在于一系列包含链条之中，就可以使用而不报错。你所说的这个原因就是这样。 3 编译器自带头文件，例如math.h、stdlib.h等等，这些头文件和你自己的定义的头文件当然不在同一级目录下，因为你的自定义头文件一般放在工程文件夹目录以下某个位置，这是不会出错的。  1、为什么同一个头文件会出现在好几个路径下面，只出现在一个路径下面不可以吗？        我的意思是在同一个工程文件的有些.c文件会出现在不同文件夹下。 2、再请问下一个.c文件里面函数的作用域是.c文件所在的当前文件夹吗？这个文件夹的上级目录的代码可以引用这个.c文件定义的函数吗？这个文件夹的下级目录呢？ 谢谢

user: user-3
content: 如果楼主用的是gcc, 可以用-E选项进行预编译，这样就能看出是include哪些文件了。头文件的搜寻路径一般以-I 或-i参数形式出现在gcc的命令行里，楼主可以看看Makefile。

user: user-1
content: 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

user: user-4
content: 1如果你所说的“几个路径”、“同一路径”是指文件目录的路径，那么自定义头文件是需要放在同一个工程文件夹路径下； 2 只要变量所在的头文件，存在于一系列包含链条之中，就可以使用而不报错。你所说的这个原因就是这样。 3 编译器自带头文件，例如math.h、stdlib.h等等，这些头文件和你自己的定义的头文件当然不在同一级目录下，因为你的自定义头文件一般放在工程文件夹目录以下某个位置，这是不会出错的。