#include 和#include“filename”之间的区别是什么?

时间:2022-11-25 10:58:03

In the C and C++ programming languages, what is the difference between using angle brackets and using quotes in an include statement, as follows?

在C和c++编程语言中,使用尖括号和在include语句中使用引号之间的区别是什么?

  1. #include <filename>
  2. # include <文件名>
  3. #include "filename"
  4. # include“文件名”

28 个解决方案

#1


958  

In practice, the difference is in the location where the preprocessor searches for the included file.

在实践中,区别在于预处理器搜索包含的文件的位置。

For #include <filename> the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.

对于#include ,预处理器在实现依赖的方式下进行搜索,通常在编译器/IDE指定的搜索目录中。此方法通常用于包含标准库头文件。

For #include "filename" the preprocessor searches first in the same directory as the file containing the directive, and then follows the search path used for the #include <filename> form. This method is normally used to include programmer-defined header files.

对于#include“filename”,预处理程序首先在与包含指令的文件相同的目录中搜索,然后跟踪用于#的搜索路径,包括 格式。此方法通常用于包含程序员定义的头文件。

A more complete description is available in the GCC documentation on search paths.

关于搜索路径的GCC文档中有更完整的描述。

#2


563  

The only way to know is to read your implementation's documentation.

唯一的方法就是阅读你的实现文档。

In the C standard, section 6.10.2, paragraphs 2 to 4 state:

在C标准中,第6.10.2节第2至4段:

  • A preprocessing directive of the form

    表单的预处理指令。

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

    searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

    通过在 <和> 分隔符之间的指定序列唯一确定的头序列,搜索一个由实现定义的位置序列,并导致该指令的全部内容由header的全部内容替换。如何指定位置或标识的头是由实现定义的。

  • 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.

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

  • A preprocessing directive of the form

    表单的预处理指令。

    #include pp-tokens new-line
    

    (that does not match one of the two previous forms) is permitted. The preprocessing tokens after include in the directive are processed just as in normal text. (Each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens.) The directive resulting after all replacements shall match one of the two previous forms. The method by which a sequence of preprocessing tokens between a < and a > preprocessing token pair or a pair of " characters is combined into a single header name preprocessing token is implementation-defined.

    (这与前两种表格中的一种不符)是允许的。在指令中包含的预处理标记与普通文本一样处理。(当前定义为宏名称的每个标识符将被替换的预处理标记列表替换。)所有替换后的指令将与前面两种形式中的一种匹配。在 <和> 预处理令牌对或一对“字符”之间的预处理标记序列组合成一个单独的头名称预处理令牌的方法是由实现定义的。

Definitions:

  • h-char: any member of the source character set except the new-line character and >

    h-char:除了新行字符和>之外,源字符集的任何成员。

  • q-char: any member of the source character set except the new-line character and "

    q-char:源字符集的任何成员,除了新行字符和"

#3


198  

The sequence of characters between < and > uniquely refer to a header, which isn't necessarily a file. Implementations are pretty much free to use the character sequence as they wish. (Mostly, however, just treat it as a file name and do a search in the include path, as the other posts state.)

<和> 之间的字符序列唯一地引用一个标题,这并不一定是一个文件。实现几乎可以随心所欲地使用字符序列。(不过,大多数情况下,只是把它当作一个文件名,并在include路径中进行搜索,就像其他posts状态一样)。

If the #include "file" form is used, the implementation first looks for a file of the given name, if supported. If not (supported), or if the search fails, the implementation behaves as though the other (#include <file>) form was used.

如果使用的是“file”表单,那么如果支持的话,实现首先查找给定名称的文件。如果没有(支持),或者如果搜索失败,那么这个实现就会像使用其他(#include )形式一样。

Also, a third form exists and is used when the #include directive doesn't match either of the forms above. In this form, some basic preprocessing (such as macro expansion) is done on the "operands" of the #include directive, and the result is expected to match one of the two other forms.

另外,还有第三种形式存在,当#include指令与上面的任何形式都不匹配时使用。在这个表单中,一些基本的预处理(比如宏扩展)是在#include指令的“操作数”上完成的,结果将与另外两个表单中的一个匹配。

#4


87  

Some good answers here make references to the C standard but forgot the POSIX standard, especially the specific behavior of the c99 (e.g. C compiler) command.

这里有一些好的答案可以参考C标准,但是忘记了POSIX标准,特别是c99(例如C编译器)命令的特定行为。

According to The Open Group Base Specifications Issue 7,

根据开放组基本规格第7期,

-I directory

我的目录

Change the algorithm for searching for headers whose names are not absolute pathnames to look in the directory named by the directory pathname before looking in the usual places. Thus, headers whose names are enclosed in double-quotes ( "" ) shall be searched for first in the directory of the file with the #include line, then in directories named in -I options, and last in the usual places. For headers whose names are enclosed in angle brackets ( "<>" ), the header shall be searched for only in directories named in -I options and then in the usual places. Directories named in -I options shall be searched in the order specified. Implementations shall support at least ten instances of this option in a single c99 command invocation.

在查找通常的位置之前,请更改搜索标题的算法,其名称并不是绝对路径名,以查看目录路径名所指定的目录。因此,将名称包含在双引号中的头(“”)将首先在文件的目录中搜索#include行,然后在-I选项中指定的目录中搜索,最后在通常的位置中查找。对于以尖括号括起来的标题(“<>”),标题只在-I选项中指定的目录中搜索,然后在通常的地方搜索。在i选项中指定的目录将按指定的顺序进行搜索。在单个c99命令调用中,实现应该至少支持该选项的10个实例。

So, in a POSIX compliant environment, with a POSIX compliant C compiler, #include "file.h" is likely going to search for ./file.h first, where . is the directory where is the file with the #include statement, while #include <file.h>, is likely going to search for /usr/include/file.h first, where /usr/include is your system defined usual places for headers (it's seems not defined by POSIX).

因此,在POSIX兼容的环境中,使用POSIX兼容的C编译器,#include“文件”。可能会搜索。/文件。h第一,。是包含#include语句的文件的目录,而#include <文件。h> 可能会搜索/usr/ include/file。首先,where /usr/include是您的系统为header定义的通常位置(它似乎不是由POSIX定义的)。

#5


35  

It does:

它:

"mypath/myfile" is short for ./mypath/myfile

with . being either the directory of the file where the #include is contained in, and/or the current working directory of the compiler, and/or the default_include_paths

与。要么是包含在#include的文件的目录,要么是编译器的当前工作目录,以及/或default_include_paths。

and

<mypath/myfile> is short for <defaultincludepaths>/mypath/myfile

If ./ is in <default_include_paths>, then it doesn't make a difference.

如果。/在 中,那么它不会产生任何影响。

If mypath/myfile is in another include directory, the behavior is undefined.

如果mypath/myfile位于另一个包含目录中,则该行为是未定义的。

#6


26  

GCC documentation says the following about the difference between the two:

GCC的文档说明了两者之间的区别:

Both user and system header files are included using the preprocessing directive ‘#include’. It has two variants:

用户和系统头文件都包括使用预处理指令' #include '。它有两个变量:

#include <file>

# include <文件>

This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the -I option (see Invocation).

此变体用于系统头文件。它在系统目录的标准列表中搜索名为file的文件。您可以使用-I选项(请参阅调用)将目录提前到此列表。

#include "file"

#包括“文件”

This variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used for <file>. You can prepend directories to the list of quote directories with the -iquote option. The argument of ‘#include’, whether delimited with quote marks or angle brackets, behaves like a string constant in that comments are not recognized, and macro names are not expanded. Thus, #include <x/*y> specifies inclusion of a system header file named x/*y.

该变体用于您自己程序的头文件。它首先在包含当前文件的目录中搜索一个名为file的文件,然后在引用目录中搜索,然后在用于 的目录中使用相同的目录。您可以使用-iquote选项将目录提前到引用目录列表。“#include”的参数,无论是用引号或尖括号分隔,在该注释中表现为字符串常量,但未被识别,宏名称没有展开。因此,#include 指定包含一个名为x/*y的系统头文件。

However, if backslashes occur within file, they are considered ordinary text characters, not escape characters. None of the character escape sequences appropriate to string constants in C are processed. Thus,#include "x\n\\y"specifies a filename containing three backslashes. (Some systems interpret ‘\’ as a pathname separator. All of these also interpret ‘/’ the same way. It is most portable to use only ‘/’.)

但是,如果在文件中出现反斜杠,则它们被认为是普通的文本字符,而不是转义字符。没有一个字符转义序列适合于C中的字符串常量。因此,#include“x\n\\y”指定包含三个反斜杠的文件名。(有些系统将“\”解释为路径名分隔符。所有这些都同样解释“/”。它是最便携的,只使用' / '。

It is an error if there is anything (other than comments) on the line after the file name.

如果在文件名后面的行中有任何东西(除了注释),这是一个错误。

#7


22  

The <file> include tells the preprocessor to search in -I directories and in predefined directories first, then in the .c file's directory. The "file" include tells the preprocessor to search the source file's directory first, and then revert to -I and predefined. All destinations are searched anyway, only the order of search is different.

<文件> 包括告诉预处理器在i目录和预定义目录中搜索,然后在.c文件的目录中搜索。“文件”包括告诉预处理器先搜索源文件的目录,然后恢复到-I和预定义。所有的目的地都被搜索,只有搜索的顺序不同。

The 2011 standard mostly discusses the include files in "16.2 Source file inclusion".

2011年的标准主要讨论了“16.2源文件包含”中包含的文件。

2 A preprocessing directive of the form

表单的预处理指令。

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

# include < h-char-sequence >新行

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

通过在 <和> 分隔符之间的指定序列唯一确定的头序列,搜索一个由实现定义的位置序列,并导致该指令的全部内容由header的全部内容替换。如何指定位置或标识的头是由实现定义的。

3 A preprocessing directive of the form

表单的预处理指令。

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

# include“q-char-sequence”新行

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

# include < h-char-sequence >新行

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

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

Note that "xxx" form degrades to <xxx> form if the file is not found. The rest is implementation-defined.

注意,如果没有找到文件,“xxx”格式将降级为 格式。其余的是由实现定义的。

#8


16  

By the standard - yes, they are different:

按照标准——是的,它们是不同的:

  • A preprocessing directive of the form

    表单的预处理指令。

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

    searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

    通过在 <和> 分隔符之间的指定序列唯一确定的头序列,搜索一个由实现定义的位置序列,并导致该指令的全部内容由header的全部内容替换。如何指定位置或标识的头是由实现定义的。

  • 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.

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

  • A preprocessing directive of the form

    表单的预处理指令。

    #include pp-tokens new-line
    

    (that does not match one of the two previous forms) is permitted. The preprocessing tokens after include in the directive are processed just as in normal text. (Each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens.) The directive resulting after all replacements shall match one of the two previous forms. The method by which a sequence of preprocessing tokens between a < and a > preprocessing token pair or a pair of " characters is combined into a single header name preprocessing token is implementation-defined.

    (这与前两种表格中的一种不符)是允许的。在指令中包含的预处理标记与普通文本一样处理。(当前定义为宏名称的每个标识符将被替换的预处理标记列表替换。)所有替换后的指令将与前面两种形式中的一种匹配。在 <和> 预处理令牌对或一对“字符”之间的预处理标记序列组合成一个单独的头名称预处理令牌的方法是由实现定义的。

Definitions:

  • h-char: any member of the source character set except the new-line character and >

    h-char:除了新行字符和>之外,源字符集的任何成员。

  • q-char: any member of the source character set except the new-line character and "

    q-char:源字符集的任何成员,除了新行字符和"

Note that the standard does not tell any relation between the implementation-defined manners. The first form searches in one implementation-defined way, and the other in a (possibly other) implementation-defined way. The standard also specifies that certain include files shall be present (for example, <stdio.h>).

注意,标准并没有说明实现定义的礼貌之间的任何关系。第一个表单在一个实现定义的方式中搜索,另一个在一个(可能是其他)实现定义的方式中进行搜索。该标准还指定了某些包含文件(例如, )。

Formally you'd have to read the manual for your compiler, however normally (by tradition) the #include "..." form searches the directory of the file in which the #include was found first, and then the directories that the #include <...> form searches (the include path, eg system headers).

正式地,您必须阅读编译器的手册,但通常(按传统)是#include“…”表单搜索文件的目录,其中#include首先被找到,然后是#include的目录。表单搜索(包含路径,如系统标题)。

#9


12  

Thanks for the great answers, esp. Adam Stelmaszczyk and piCookie, and aib.

感谢伟大的答案,esp. Adam Stelmaszczyk和piCookie,和aib。

Like many programmers, I have used the informal convention of using the "myApp.hpp" form for application specific files, and the <libHeader.hpp> form for library and compiler system files, i.e. files specified in /I and the INCLUDE environment variable, for years thinking that was the standard.

和许多程序员一样,我使用了“myApp”的非正式约定。hpp“用于应用特定的文件,以及 表单,即在/I中指定的文件和包含环境变量的文件,多年来一直认为这是标准。 ”。用于库和编译器系统文件的hpp>

However, the C standard states that the search order is implementation specific, which can make portability complicated. To make matters worse, we use jam, which automagically figures out where the include files are. You can use relative or absolute paths for your include files. i.e.

然而,C标准指出搜索顺序是特定于实现的,这使得可移植性变得复杂。更糟糕的是,我们使用jam,它自动计算出包含文件的位置。您可以为包含文件使用相对或绝对路径。即。

#include "../../MyProgDir/SourceDir1/someFile.hpp"

Older versions of MSVS required double backslashes (\\), but now that's not required. I don't know when it changed. Just use forward slashes for compatibility with 'nix (Windows will accept that).

旧版本的MSVS需要双反斜杠(\\),但现在这不是必需的。我不知道它什么时候变了。使用前斜杠来与“nix”兼容(Windows将接受这一点)。

If you are really worried about it, use "./myHeader.h" for an include file in the same directory as the source code (my current, very large project has some duplicate include file names scattered about--really a configuration management problem).

如果你真的很担心,那就用“。/myHeader”。h“对于一个包含与源代码相同的目录中的文件(我当前的,非常大的项目有一些副本,包括分散的文件名称——真正的配置管理问题)。

Here's the MSDN explanation copied here for your convenience).

这里是为了您的方便而复制的MSDN的解释。

Quoted form

引用形式

The preprocessor searches for include files in this order:

预处理器搜索包含以下命令中的文件:

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

Angle-bracket form

后面的形式

The preprocessor searches for include files in this order:

预处理器搜索包含以下命令中的文件:

  1. Along the path that's specified by each /I compiler option.
  2. 沿着每个/I编译器选项指定的路径。
  3. When compiling occurs on the command line, along the paths that are specified by the INCLUDE environment variable.
  4. 当编译发生在命令行上时,沿着包含环境变量指定的路径。

#10


11  

At least for GCC version <= 3.0, the angle-bracket form does not generate a dependency between the included file and the including one.

至少在GCC版本<= 3.0中,角括号窗体不会在包含的文件和包含的文件之间产生依赖关系。

So if you want to generate dependency rules (using the GCC -M option for exemple), you must use the quoted form for the files that should be included in the dependency tree.

因此,如果您想要生成依赖项规则(例如使用GCC -M选项),您必须使用包含在从属树中的文件的引用形式。

(See http://gcc.gnu.org/onlinedocs/cpp/Invocation.html )

(参见http://gcc.gnu.org/onlinedocs/cpp/Invocation.html)

#11


9  

For #include "" a compiler normally searches the folder of the file which contains that include and then the other folders. For #include <> the compiler does not search the current file's folder.

对于#include“”,编译器通常会搜索包含该文件的文件的文件夹,然后再搜索其他文件夹。对于#include <>,编译器不会搜索当前文件的文件夹。

#12


7  

An #include with angle brackets will search an "implementation-dependent list of places" (which is a very complicated way of saying "system headers") for the file to be included.

一个包含尖括号的#将搜索一个“特定于实现的位置列表”(这是一种非常复杂的“系统标头”方式)。

An #include with quotes will just search for a file (and, "in an implementation-dependent manner", bleh). Which means, in normal English, it will try to apply the path/filename that you toss at it and will not prepend a system path or tamper with it otherwise.

包含引号的#将只搜索一个文件(以及“以实现依赖的方式”,bleh)。也就是说,在正常的英语中,它会尝试应用程序的路径/文件名,而不会预先设置系统路径或篡改它。

Also, if #include "" fails, it is re-read as #include <> by the standard.

另外,如果#include“”失败,它将被重新读取为#include <>。

The gcc documentation has a (compiler specific) description which although being specific to gcc and not the standard, is a lot easier to understand than the attorney-style talk of the ISO standards.

gcc文档有一个(特定于编译器的)描述,尽管它是特定于gcc而不是标准的,但它比ISO标准的律师风格的讨论要容易理解得多。

#13


7  

#include "filename" // User defined header
#include <filename> // Standard library header.

Example:

例子:

The filename here is Seller.h:

这里的文件名是Seller.h:

#ifndef SELLER_H     // Header guard
#define SELLER_H     // Header guard

#include <string>
#include <iostream>
#include <iomanip>

class Seller
{
    private:
        char name[31];
        double sales_total;

    public:
        Seller();
        Seller(char[], double);
        char*getName();

#endif

In the class implementation (for example, Seller.cpp, and in other files that will use the file Seller.h), the header defined by the user should now be included, as follows:

在类实现中(例如,卖方)。cpp,以及其他将使用文件Seller.h的文件,用户定义的头现在应该包括如下:

#include "Seller.h"

#14


7  

  • #include <> is for predefined header files
  • #include <>是用于预定义的头文件。

If the header file is predefined then you would simply write the header file name in angular brackets, and it would look like this (assuming we have a predefined header file name iostream):

如果头文件是预定义的,那么您只需在尖括号中写入头文件名称,它看起来就像这样(假设我们有一个预定义的头文件名称iostream):

#include <iostream>
  • #include " " is for header files the programmer defines
  • #include“”是程序员定义的头文件。

If you (the programmer) wrote your own header file then you would write the header file name in quotes. So, suppose you wrote a header file called myfile.h, then this is an example of how you would use the include directive to include that file:

如果您(程序员)编写了自己的头文件,那么您将在引号中写入头文件名称。假设您编写了一个名为myfile的头文件。h,这是一个例子说明你如何使用include指令来包含这个文件:

#include "myfile.h"

#15


6  

Many of the answers here focus on the paths the compiler will search in order to find the file. While this is what most compilers do, a conforming compiler is allowed to be preprogrammed with the effects of the standard headers, and to treat, say, #include <list> as a switch, and it need not exist as a file at all.

这里的许多答案都集中在编译器为了找到该文件而搜索的路径上。虽然这是大多数编译器所做的事情,但是一个符合标准的编译器可以被预先编程,并使用标准头文件的效果,并且将#include 作为一个开关,并且它根本不需要作为一个文件存在。

This is not purely hypothetical. There is at least one compiler that work that way. Using #include <xxx> only with standard headers is recommended.

这不是纯粹的假设。至少有一个编译器是这样工作的。使用#include ,只建议使用标准标题。

#16


4  

In C++, include a file in two ways:

在c++中,以两种方式包含一个文件:

The first one is #include which tells the preprocessor to look for the file in the predefined default location. This location is often an INCLUDE environment variable that denotes the path to include files.

第一个是#include,它告诉预处理器在预定义的默认位置查找文件。这个位置通常包含一个环境变量,它表示包含文件的路径。

And the second type is #include "filename" which tells the preprocessor to look for the file in the current directory first, then look for it in the predefined locations user have set up.

第二种类型是#include“filename”,它告诉预处理器先在当前目录中查找文件,然后在用户设置的预定义位置查找该文件。

#17


4  

#include <abc.h>

is used to include standard library files. So the compiler will check in the locations where standard library headers are residing.

用于包含标准库文件。因此,编译器将在标准库头所在的位置进行检查。

#include "xyz.h"

will tell the compiler to include user-defined header files. So the compiler will check for these header files in the current folder or -I defined folders.

将告诉编译器包含用户定义的头文件。因此,编译器将检查当前文件夹或我定义的文件夹中的这些头文件。

#18


3  

The #include <filename> is used when a system file is being referred to. That is a header file that can be found at system default locations like /usr/include or /usr/local/include. For your own files that needs to be included in another program you have to use the #include "filename" syntax.

#include 是在系统文件被引用时使用的。这是一个头文件,可以在系统缺省位置(如/usr/include或/usr/ local/include)中找到。对于需要包含在另一个程序中的文件,您必须使用#include“filename”语法。

#19


2  

The simple general rule is to use angled brackets to include header files that come with the compiler. Use double quotes to include any other header files. Most compilers do it this way.

简单的一般规则是使用尖括号来包含与编译器一起出现的头文件。使用双引号包含任何其他头文件。大多数编译器都这样做。

1.9 — Header files explains in more detail about pre-processor directives. If you are a novice programmer, that page should help you understand all that. I learned it from here, and I have been following it at work.

1.9 -头文件更详细地解释了预处理程序指令。如果你是一个新手程序员,这个页面应该会帮助你理解所有这些。我是从这里学到的,我一直在研究它。

#20


1  

#include <filename>

is used when you want to use the header file of the C/C++ system or compiler libraries. These libraries can be stdio.h, string.h, math.h, etc.

当您希望使用C/ c++系统或编译器库的头文件时使用。这些库可以是stdio。h,字符串。h,数学。h等。

#include "path-to-file/filename"

is used when you want to use your own custom header file which is in your project folder or somewhere else.

当您想要使用您自己的自定义头文件时,在您的项目文件夹或其他地方使用。

For more information about preprocessors and header. Read C - Preprocessors.

有关预处理器和标题的更多信息。读取C -预处理器。

#21


0  

I believe that headers included in double-quotes will be looked for the in same system paths as angle-bracketed includes if they are not found in the current directory.

我认为,双引号中包含的头信息将在相同的系统路径中查找,如果它们在当前目录中没有找到的话,则会被查找。

#22


0  

  #include <filename>   (1)     
  #include "filename"   (2)

#include includes source file, identified by filename, into the current source file at the line immediately after the directive.

#include包括源文件,由filename标识,在指令后立即进入当前的源文件。

The first version of the directive searches only standard include directories. The standard C++ library, as well as standard C library, is implicitly included in standard include directories. The standard include directories can be controlled by the user through compiler options.

指令搜索的第一个版本只搜索标准目录。标准的c++库,以及标准的C库,都隐式地包含在标准包含目录中。标准包括目录可以由用户通过编译器选项控制。

The second version first searches the directory where the current file resides and, only if the file is not found, searches the standard include directories.

第二个版本首先搜索当前文件所在的目录,如果没有找到该文件,则搜索标准包括目录。

In the case the file is not found, the program is ill-formed.

在没有找到文件的情况下,程序是不正确的。

#23


0  

For #include "filename" the preprocessor searches in the same directory as the file containing the directive. This method is normally used to include programmer-defined header files.

对于#include“filename”,预处理程序在与包含指令的文件相同的目录中搜索。此方法通常用于包含程序员定义的头文件。

For #include the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.

对于#,在实现依赖的方式中包含预处理器搜索,通常在编译器/IDE指定的搜索目录中。此方法通常用于包含标准库头文件。

#24


0  

#include <file> tells the compiler to search for the header in its includes directory, e.g. for MinGW the compiler would search for file in C:\MinGW\include\ or wherever your compiler is installed.

#include <文件> 告诉编译器在其包含的目录中搜索头信息,例如:for MinGW,编译器会在C:\MinGW\包括\或任何安装编译器的地方搜索文件。

#include "file" tells the compiler to search the current directory (i.e. the directory in which the source file resides) for file.

#include“文件”告诉编译器搜索当前目录(即源文件所在的目录)。

You can use the -I flag for GCC to tell it that, when it encounters an include with angled brackets, it should also search for headers in the directory after -I. For instance, if you have a file called myheader.h in your own directory, you could say #include <myheader.h> if you called GCC with -I . (indicating that it should search for includes in the current directory.)

您可以使用-I标志来告诉GCC,当它遇到带有尖括号的include时,它也应该在-I之后搜索目录中的头。例如,如果您有一个名为myheader的文件。在您自己的目录中,您可以说#include 。如果你把gcc称为-i。(说明它应该在当前目录中搜索包含的内容。)

#25


0  

the " < filename > " searches in standard C library locations

在标准C库位置搜索“< filename >”。

whereas "filename" searches in the current directory as well.

而“filename”在当前目录中搜索。

Ideally, you would use <...> for standard C libraries and "..." for libraries that you write and are present in the current directory.

理想情况下,您将使用<…用于标准C库的>和您在当前目录中所写的库的“…”。

#26


-1  

The order of search header files is different. <XXX.h> prefer to search the standard headers first while "XXX.h" searches the workspace's header files first.

搜索头文件的顺序是不同的。< XXX。h>更倾向于先搜索标准头文件,而“XXX”。h“首先搜索工作空间的头文件。

#27


-1  

#include <filename>

# include <文件名>

will find the corresponding file from the C++ library. it means if you have a file called hello.h in the C++ library folder, #include <hello.h> will load it.

将从c++库中找到相应的文件。这意味着如果你有一个文件叫hello。在c++库文件夹中,#include 会加载它。 。h>

But,

但是,

#include "filename"

# include“文件名”

will find the file in the same directory where your source file is.

将在源文件所在的目录中找到该文件。

In addition,

此外,

#include "path_to_file/filename"

# include“path_to_file /文件名”

will find the file in the directory which you typed in path_to_file.

将在path_to_file中键入的目录中找到该文件。

#28


-3  

To include a predefined library header file , #include<filename> is used whereas to include user defined header file, #include "filename" is relevant.

要包含预定义的库头文件,#include 被使用,而包括用户定义的头文件,#include“filename”是相关的。

#1


958  

In practice, the difference is in the location where the preprocessor searches for the included file.

在实践中,区别在于预处理器搜索包含的文件的位置。

For #include <filename> the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.

对于#include ,预处理器在实现依赖的方式下进行搜索,通常在编译器/IDE指定的搜索目录中。此方法通常用于包含标准库头文件。

For #include "filename" the preprocessor searches first in the same directory as the file containing the directive, and then follows the search path used for the #include <filename> form. This method is normally used to include programmer-defined header files.

对于#include“filename”,预处理程序首先在与包含指令的文件相同的目录中搜索,然后跟踪用于#的搜索路径,包括 格式。此方法通常用于包含程序员定义的头文件。

A more complete description is available in the GCC documentation on search paths.

关于搜索路径的GCC文档中有更完整的描述。

#2


563  

The only way to know is to read your implementation's documentation.

唯一的方法就是阅读你的实现文档。

In the C standard, section 6.10.2, paragraphs 2 to 4 state:

在C标准中,第6.10.2节第2至4段:

  • A preprocessing directive of the form

    表单的预处理指令。

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

    searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

    通过在 <和> 分隔符之间的指定序列唯一确定的头序列,搜索一个由实现定义的位置序列,并导致该指令的全部内容由header的全部内容替换。如何指定位置或标识的头是由实现定义的。

  • 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.

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

  • A preprocessing directive of the form

    表单的预处理指令。

    #include pp-tokens new-line
    

    (that does not match one of the two previous forms) is permitted. The preprocessing tokens after include in the directive are processed just as in normal text. (Each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens.) The directive resulting after all replacements shall match one of the two previous forms. The method by which a sequence of preprocessing tokens between a < and a > preprocessing token pair or a pair of " characters is combined into a single header name preprocessing token is implementation-defined.

    (这与前两种表格中的一种不符)是允许的。在指令中包含的预处理标记与普通文本一样处理。(当前定义为宏名称的每个标识符将被替换的预处理标记列表替换。)所有替换后的指令将与前面两种形式中的一种匹配。在 <和> 预处理令牌对或一对“字符”之间的预处理标记序列组合成一个单独的头名称预处理令牌的方法是由实现定义的。

Definitions:

  • h-char: any member of the source character set except the new-line character and >

    h-char:除了新行字符和>之外,源字符集的任何成员。

  • q-char: any member of the source character set except the new-line character and "

    q-char:源字符集的任何成员,除了新行字符和"

#3


198  

The sequence of characters between < and > uniquely refer to a header, which isn't necessarily a file. Implementations are pretty much free to use the character sequence as they wish. (Mostly, however, just treat it as a file name and do a search in the include path, as the other posts state.)

<和> 之间的字符序列唯一地引用一个标题,这并不一定是一个文件。实现几乎可以随心所欲地使用字符序列。(不过,大多数情况下,只是把它当作一个文件名,并在include路径中进行搜索,就像其他posts状态一样)。

If the #include "file" form is used, the implementation first looks for a file of the given name, if supported. If not (supported), or if the search fails, the implementation behaves as though the other (#include <file>) form was used.

如果使用的是“file”表单,那么如果支持的话,实现首先查找给定名称的文件。如果没有(支持),或者如果搜索失败,那么这个实现就会像使用其他(#include )形式一样。

Also, a third form exists and is used when the #include directive doesn't match either of the forms above. In this form, some basic preprocessing (such as macro expansion) is done on the "operands" of the #include directive, and the result is expected to match one of the two other forms.

另外,还有第三种形式存在,当#include指令与上面的任何形式都不匹配时使用。在这个表单中,一些基本的预处理(比如宏扩展)是在#include指令的“操作数”上完成的,结果将与另外两个表单中的一个匹配。

#4


87  

Some good answers here make references to the C standard but forgot the POSIX standard, especially the specific behavior of the c99 (e.g. C compiler) command.

这里有一些好的答案可以参考C标准,但是忘记了POSIX标准,特别是c99(例如C编译器)命令的特定行为。

According to The Open Group Base Specifications Issue 7,

根据开放组基本规格第7期,

-I directory

我的目录

Change the algorithm for searching for headers whose names are not absolute pathnames to look in the directory named by the directory pathname before looking in the usual places. Thus, headers whose names are enclosed in double-quotes ( "" ) shall be searched for first in the directory of the file with the #include line, then in directories named in -I options, and last in the usual places. For headers whose names are enclosed in angle brackets ( "<>" ), the header shall be searched for only in directories named in -I options and then in the usual places. Directories named in -I options shall be searched in the order specified. Implementations shall support at least ten instances of this option in a single c99 command invocation.

在查找通常的位置之前,请更改搜索标题的算法,其名称并不是绝对路径名,以查看目录路径名所指定的目录。因此,将名称包含在双引号中的头(“”)将首先在文件的目录中搜索#include行,然后在-I选项中指定的目录中搜索,最后在通常的位置中查找。对于以尖括号括起来的标题(“<>”),标题只在-I选项中指定的目录中搜索,然后在通常的地方搜索。在i选项中指定的目录将按指定的顺序进行搜索。在单个c99命令调用中,实现应该至少支持该选项的10个实例。

So, in a POSIX compliant environment, with a POSIX compliant C compiler, #include "file.h" is likely going to search for ./file.h first, where . is the directory where is the file with the #include statement, while #include <file.h>, is likely going to search for /usr/include/file.h first, where /usr/include is your system defined usual places for headers (it's seems not defined by POSIX).

因此,在POSIX兼容的环境中,使用POSIX兼容的C编译器,#include“文件”。可能会搜索。/文件。h第一,。是包含#include语句的文件的目录,而#include <文件。h> 可能会搜索/usr/ include/file。首先,where /usr/include是您的系统为header定义的通常位置(它似乎不是由POSIX定义的)。

#5


35  

It does:

它:

"mypath/myfile" is short for ./mypath/myfile

with . being either the directory of the file where the #include is contained in, and/or the current working directory of the compiler, and/or the default_include_paths

与。要么是包含在#include的文件的目录,要么是编译器的当前工作目录,以及/或default_include_paths。

and

<mypath/myfile> is short for <defaultincludepaths>/mypath/myfile

If ./ is in <default_include_paths>, then it doesn't make a difference.

如果。/在 中,那么它不会产生任何影响。

If mypath/myfile is in another include directory, the behavior is undefined.

如果mypath/myfile位于另一个包含目录中,则该行为是未定义的。

#6


26  

GCC documentation says the following about the difference between the two:

GCC的文档说明了两者之间的区别:

Both user and system header files are included using the preprocessing directive ‘#include’. It has two variants:

用户和系统头文件都包括使用预处理指令' #include '。它有两个变量:

#include <file>

# include <文件>

This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the -I option (see Invocation).

此变体用于系统头文件。它在系统目录的标准列表中搜索名为file的文件。您可以使用-I选项(请参阅调用)将目录提前到此列表。

#include "file"

#包括“文件”

This variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used for <file>. You can prepend directories to the list of quote directories with the -iquote option. The argument of ‘#include’, whether delimited with quote marks or angle brackets, behaves like a string constant in that comments are not recognized, and macro names are not expanded. Thus, #include <x/*y> specifies inclusion of a system header file named x/*y.

该变体用于您自己程序的头文件。它首先在包含当前文件的目录中搜索一个名为file的文件,然后在引用目录中搜索,然后在用于 的目录中使用相同的目录。您可以使用-iquote选项将目录提前到引用目录列表。“#include”的参数,无论是用引号或尖括号分隔,在该注释中表现为字符串常量,但未被识别,宏名称没有展开。因此,#include 指定包含一个名为x/*y的系统头文件。

However, if backslashes occur within file, they are considered ordinary text characters, not escape characters. None of the character escape sequences appropriate to string constants in C are processed. Thus,#include "x\n\\y"specifies a filename containing three backslashes. (Some systems interpret ‘\’ as a pathname separator. All of these also interpret ‘/’ the same way. It is most portable to use only ‘/’.)

但是,如果在文件中出现反斜杠,则它们被认为是普通的文本字符,而不是转义字符。没有一个字符转义序列适合于C中的字符串常量。因此,#include“x\n\\y”指定包含三个反斜杠的文件名。(有些系统将“\”解释为路径名分隔符。所有这些都同样解释“/”。它是最便携的,只使用' / '。

It is an error if there is anything (other than comments) on the line after the file name.

如果在文件名后面的行中有任何东西(除了注释),这是一个错误。

#7


22  

The <file> include tells the preprocessor to search in -I directories and in predefined directories first, then in the .c file's directory. The "file" include tells the preprocessor to search the source file's directory first, and then revert to -I and predefined. All destinations are searched anyway, only the order of search is different.

<文件> 包括告诉预处理器在i目录和预定义目录中搜索,然后在.c文件的目录中搜索。“文件”包括告诉预处理器先搜索源文件的目录,然后恢复到-I和预定义。所有的目的地都被搜索,只有搜索的顺序不同。

The 2011 standard mostly discusses the include files in "16.2 Source file inclusion".

2011年的标准主要讨论了“16.2源文件包含”中包含的文件。

2 A preprocessing directive of the form

表单的预处理指令。

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

# include < h-char-sequence >新行

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

通过在 <和> 分隔符之间的指定序列唯一确定的头序列,搜索一个由实现定义的位置序列,并导致该指令的全部内容由header的全部内容替换。如何指定位置或标识的头是由实现定义的。

3 A preprocessing directive of the form

表单的预处理指令。

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

# include“q-char-sequence”新行

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

# include < h-char-sequence >新行

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

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

Note that "xxx" form degrades to <xxx> form if the file is not found. The rest is implementation-defined.

注意,如果没有找到文件,“xxx”格式将降级为 格式。其余的是由实现定义的。

#8


16  

By the standard - yes, they are different:

按照标准——是的,它们是不同的:

  • A preprocessing directive of the form

    表单的预处理指令。

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

    searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

    通过在 <和> 分隔符之间的指定序列唯一确定的头序列,搜索一个由实现定义的位置序列,并导致该指令的全部内容由header的全部内容替换。如何指定位置或标识的头是由实现定义的。

  • 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.

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

  • A preprocessing directive of the form

    表单的预处理指令。

    #include pp-tokens new-line
    

    (that does not match one of the two previous forms) is permitted. The preprocessing tokens after include in the directive are processed just as in normal text. (Each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens.) The directive resulting after all replacements shall match one of the two previous forms. The method by which a sequence of preprocessing tokens between a < and a > preprocessing token pair or a pair of " characters is combined into a single header name preprocessing token is implementation-defined.

    (这与前两种表格中的一种不符)是允许的。在指令中包含的预处理标记与普通文本一样处理。(当前定义为宏名称的每个标识符将被替换的预处理标记列表替换。)所有替换后的指令将与前面两种形式中的一种匹配。在 <和> 预处理令牌对或一对“字符”之间的预处理标记序列组合成一个单独的头名称预处理令牌的方法是由实现定义的。

Definitions:

  • h-char: any member of the source character set except the new-line character and >

    h-char:除了新行字符和>之外,源字符集的任何成员。

  • q-char: any member of the source character set except the new-line character and "

    q-char:源字符集的任何成员,除了新行字符和"

Note that the standard does not tell any relation between the implementation-defined manners. The first form searches in one implementation-defined way, and the other in a (possibly other) implementation-defined way. The standard also specifies that certain include files shall be present (for example, <stdio.h>).

注意,标准并没有说明实现定义的礼貌之间的任何关系。第一个表单在一个实现定义的方式中搜索,另一个在一个(可能是其他)实现定义的方式中进行搜索。该标准还指定了某些包含文件(例如, )。

Formally you'd have to read the manual for your compiler, however normally (by tradition) the #include "..." form searches the directory of the file in which the #include was found first, and then the directories that the #include <...> form searches (the include path, eg system headers).

正式地,您必须阅读编译器的手册,但通常(按传统)是#include“…”表单搜索文件的目录,其中#include首先被找到,然后是#include的目录。表单搜索(包含路径,如系统标题)。

#9


12  

Thanks for the great answers, esp. Adam Stelmaszczyk and piCookie, and aib.

感谢伟大的答案,esp. Adam Stelmaszczyk和piCookie,和aib。

Like many programmers, I have used the informal convention of using the "myApp.hpp" form for application specific files, and the <libHeader.hpp> form for library and compiler system files, i.e. files specified in /I and the INCLUDE environment variable, for years thinking that was the standard.

和许多程序员一样,我使用了“myApp”的非正式约定。hpp“用于应用特定的文件,以及 表单,即在/I中指定的文件和包含环境变量的文件,多年来一直认为这是标准。 ”。用于库和编译器系统文件的hpp>

However, the C standard states that the search order is implementation specific, which can make portability complicated. To make matters worse, we use jam, which automagically figures out where the include files are. You can use relative or absolute paths for your include files. i.e.

然而,C标准指出搜索顺序是特定于实现的,这使得可移植性变得复杂。更糟糕的是,我们使用jam,它自动计算出包含文件的位置。您可以为包含文件使用相对或绝对路径。即。

#include "../../MyProgDir/SourceDir1/someFile.hpp"

Older versions of MSVS required double backslashes (\\), but now that's not required. I don't know when it changed. Just use forward slashes for compatibility with 'nix (Windows will accept that).

旧版本的MSVS需要双反斜杠(\\),但现在这不是必需的。我不知道它什么时候变了。使用前斜杠来与“nix”兼容(Windows将接受这一点)。

If you are really worried about it, use "./myHeader.h" for an include file in the same directory as the source code (my current, very large project has some duplicate include file names scattered about--really a configuration management problem).

如果你真的很担心,那就用“。/myHeader”。h“对于一个包含与源代码相同的目录中的文件(我当前的,非常大的项目有一些副本,包括分散的文件名称——真正的配置管理问题)。

Here's the MSDN explanation copied here for your convenience).

这里是为了您的方便而复制的MSDN的解释。

Quoted form

引用形式

The preprocessor searches for include files in this order:

预处理器搜索包含以下命令中的文件:

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

Angle-bracket form

后面的形式

The preprocessor searches for include files in this order:

预处理器搜索包含以下命令中的文件:

  1. Along the path that's specified by each /I compiler option.
  2. 沿着每个/I编译器选项指定的路径。
  3. When compiling occurs on the command line, along the paths that are specified by the INCLUDE environment variable.
  4. 当编译发生在命令行上时,沿着包含环境变量指定的路径。

#10


11  

At least for GCC version <= 3.0, the angle-bracket form does not generate a dependency between the included file and the including one.

至少在GCC版本<= 3.0中,角括号窗体不会在包含的文件和包含的文件之间产生依赖关系。

So if you want to generate dependency rules (using the GCC -M option for exemple), you must use the quoted form for the files that should be included in the dependency tree.

因此,如果您想要生成依赖项规则(例如使用GCC -M选项),您必须使用包含在从属树中的文件的引用形式。

(See http://gcc.gnu.org/onlinedocs/cpp/Invocation.html )

(参见http://gcc.gnu.org/onlinedocs/cpp/Invocation.html)

#11


9  

For #include "" a compiler normally searches the folder of the file which contains that include and then the other folders. For #include <> the compiler does not search the current file's folder.

对于#include“”,编译器通常会搜索包含该文件的文件的文件夹,然后再搜索其他文件夹。对于#include <>,编译器不会搜索当前文件的文件夹。

#12


7  

An #include with angle brackets will search an "implementation-dependent list of places" (which is a very complicated way of saying "system headers") for the file to be included.

一个包含尖括号的#将搜索一个“特定于实现的位置列表”(这是一种非常复杂的“系统标头”方式)。

An #include with quotes will just search for a file (and, "in an implementation-dependent manner", bleh). Which means, in normal English, it will try to apply the path/filename that you toss at it and will not prepend a system path or tamper with it otherwise.

包含引号的#将只搜索一个文件(以及“以实现依赖的方式”,bleh)。也就是说,在正常的英语中,它会尝试应用程序的路径/文件名,而不会预先设置系统路径或篡改它。

Also, if #include "" fails, it is re-read as #include <> by the standard.

另外,如果#include“”失败,它将被重新读取为#include <>。

The gcc documentation has a (compiler specific) description which although being specific to gcc and not the standard, is a lot easier to understand than the attorney-style talk of the ISO standards.

gcc文档有一个(特定于编译器的)描述,尽管它是特定于gcc而不是标准的,但它比ISO标准的律师风格的讨论要容易理解得多。

#13


7  

#include "filename" // User defined header
#include <filename> // Standard library header.

Example:

例子:

The filename here is Seller.h:

这里的文件名是Seller.h:

#ifndef SELLER_H     // Header guard
#define SELLER_H     // Header guard

#include <string>
#include <iostream>
#include <iomanip>

class Seller
{
    private:
        char name[31];
        double sales_total;

    public:
        Seller();
        Seller(char[], double);
        char*getName();

#endif

In the class implementation (for example, Seller.cpp, and in other files that will use the file Seller.h), the header defined by the user should now be included, as follows:

在类实现中(例如,卖方)。cpp,以及其他将使用文件Seller.h的文件,用户定义的头现在应该包括如下:

#include "Seller.h"

#14


7  

  • #include <> is for predefined header files
  • #include <>是用于预定义的头文件。

If the header file is predefined then you would simply write the header file name in angular brackets, and it would look like this (assuming we have a predefined header file name iostream):

如果头文件是预定义的,那么您只需在尖括号中写入头文件名称,它看起来就像这样(假设我们有一个预定义的头文件名称iostream):

#include <iostream>
  • #include " " is for header files the programmer defines
  • #include“”是程序员定义的头文件。

If you (the programmer) wrote your own header file then you would write the header file name in quotes. So, suppose you wrote a header file called myfile.h, then this is an example of how you would use the include directive to include that file:

如果您(程序员)编写了自己的头文件,那么您将在引号中写入头文件名称。假设您编写了一个名为myfile的头文件。h,这是一个例子说明你如何使用include指令来包含这个文件:

#include "myfile.h"

#15


6  

Many of the answers here focus on the paths the compiler will search in order to find the file. While this is what most compilers do, a conforming compiler is allowed to be preprogrammed with the effects of the standard headers, and to treat, say, #include <list> as a switch, and it need not exist as a file at all.

这里的许多答案都集中在编译器为了找到该文件而搜索的路径上。虽然这是大多数编译器所做的事情,但是一个符合标准的编译器可以被预先编程,并使用标准头文件的效果,并且将#include 作为一个开关,并且它根本不需要作为一个文件存在。

This is not purely hypothetical. There is at least one compiler that work that way. Using #include <xxx> only with standard headers is recommended.

这不是纯粹的假设。至少有一个编译器是这样工作的。使用#include ,只建议使用标准标题。

#16


4  

In C++, include a file in two ways:

在c++中,以两种方式包含一个文件:

The first one is #include which tells the preprocessor to look for the file in the predefined default location. This location is often an INCLUDE environment variable that denotes the path to include files.

第一个是#include,它告诉预处理器在预定义的默认位置查找文件。这个位置通常包含一个环境变量,它表示包含文件的路径。

And the second type is #include "filename" which tells the preprocessor to look for the file in the current directory first, then look for it in the predefined locations user have set up.

第二种类型是#include“filename”,它告诉预处理器先在当前目录中查找文件,然后在用户设置的预定义位置查找该文件。

#17


4  

#include <abc.h>

is used to include standard library files. So the compiler will check in the locations where standard library headers are residing.

用于包含标准库文件。因此,编译器将在标准库头所在的位置进行检查。

#include "xyz.h"

will tell the compiler to include user-defined header files. So the compiler will check for these header files in the current folder or -I defined folders.

将告诉编译器包含用户定义的头文件。因此,编译器将检查当前文件夹或我定义的文件夹中的这些头文件。

#18


3  

The #include <filename> is used when a system file is being referred to. That is a header file that can be found at system default locations like /usr/include or /usr/local/include. For your own files that needs to be included in another program you have to use the #include "filename" syntax.

#include 是在系统文件被引用时使用的。这是一个头文件,可以在系统缺省位置(如/usr/include或/usr/ local/include)中找到。对于需要包含在另一个程序中的文件,您必须使用#include“filename”语法。

#19


2  

The simple general rule is to use angled brackets to include header files that come with the compiler. Use double quotes to include any other header files. Most compilers do it this way.

简单的一般规则是使用尖括号来包含与编译器一起出现的头文件。使用双引号包含任何其他头文件。大多数编译器都这样做。

1.9 — Header files explains in more detail about pre-processor directives. If you are a novice programmer, that page should help you understand all that. I learned it from here, and I have been following it at work.

1.9 -头文件更详细地解释了预处理程序指令。如果你是一个新手程序员,这个页面应该会帮助你理解所有这些。我是从这里学到的,我一直在研究它。

#20


1  

#include <filename>

is used when you want to use the header file of the C/C++ system or compiler libraries. These libraries can be stdio.h, string.h, math.h, etc.

当您希望使用C/ c++系统或编译器库的头文件时使用。这些库可以是stdio。h,字符串。h,数学。h等。

#include "path-to-file/filename"

is used when you want to use your own custom header file which is in your project folder or somewhere else.

当您想要使用您自己的自定义头文件时,在您的项目文件夹或其他地方使用。

For more information about preprocessors and header. Read C - Preprocessors.

有关预处理器和标题的更多信息。读取C -预处理器。

#21


0  

I believe that headers included in double-quotes will be looked for the in same system paths as angle-bracketed includes if they are not found in the current directory.

我认为,双引号中包含的头信息将在相同的系统路径中查找,如果它们在当前目录中没有找到的话,则会被查找。

#22


0  

  #include <filename>   (1)     
  #include "filename"   (2)

#include includes source file, identified by filename, into the current source file at the line immediately after the directive.

#include包括源文件,由filename标识,在指令后立即进入当前的源文件。

The first version of the directive searches only standard include directories. The standard C++ library, as well as standard C library, is implicitly included in standard include directories. The standard include directories can be controlled by the user through compiler options.

指令搜索的第一个版本只搜索标准目录。标准的c++库,以及标准的C库,都隐式地包含在标准包含目录中。标准包括目录可以由用户通过编译器选项控制。

The second version first searches the directory where the current file resides and, only if the file is not found, searches the standard include directories.

第二个版本首先搜索当前文件所在的目录,如果没有找到该文件,则搜索标准包括目录。

In the case the file is not found, the program is ill-formed.

在没有找到文件的情况下,程序是不正确的。

#23


0  

For #include "filename" the preprocessor searches in the same directory as the file containing the directive. This method is normally used to include programmer-defined header files.

对于#include“filename”,预处理程序在与包含指令的文件相同的目录中搜索。此方法通常用于包含程序员定义的头文件。

For #include the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.

对于#,在实现依赖的方式中包含预处理器搜索,通常在编译器/IDE指定的搜索目录中。此方法通常用于包含标准库头文件。

#24


0  

#include <file> tells the compiler to search for the header in its includes directory, e.g. for MinGW the compiler would search for file in C:\MinGW\include\ or wherever your compiler is installed.

#include <文件> 告诉编译器在其包含的目录中搜索头信息,例如:for MinGW,编译器会在C:\MinGW\包括\或任何安装编译器的地方搜索文件。

#include "file" tells the compiler to search the current directory (i.e. the directory in which the source file resides) for file.

#include“文件”告诉编译器搜索当前目录(即源文件所在的目录)。

You can use the -I flag for GCC to tell it that, when it encounters an include with angled brackets, it should also search for headers in the directory after -I. For instance, if you have a file called myheader.h in your own directory, you could say #include <myheader.h> if you called GCC with -I . (indicating that it should search for includes in the current directory.)

您可以使用-I标志来告诉GCC,当它遇到带有尖括号的include时,它也应该在-I之后搜索目录中的头。例如,如果您有一个名为myheader的文件。在您自己的目录中,您可以说#include 。如果你把gcc称为-i。(说明它应该在当前目录中搜索包含的内容。)

#25


0  

the " < filename > " searches in standard C library locations

在标准C库位置搜索“< filename >”。

whereas "filename" searches in the current directory as well.

而“filename”在当前目录中搜索。

Ideally, you would use <...> for standard C libraries and "..." for libraries that you write and are present in the current directory.

理想情况下,您将使用<…用于标准C库的>和您在当前目录中所写的库的“…”。

#26


-1  

The order of search header files is different. <XXX.h> prefer to search the standard headers first while "XXX.h" searches the workspace's header files first.

搜索头文件的顺序是不同的。< XXX。h>更倾向于先搜索标准头文件,而“XXX”。h“首先搜索工作空间的头文件。

#27


-1  

#include <filename>

# include <文件名>

will find the corresponding file from the C++ library. it means if you have a file called hello.h in the C++ library folder, #include <hello.h> will load it.

将从c++库中找到相应的文件。这意味着如果你有一个文件叫hello。在c++库文件夹中,#include 会加载它。 。h>

But,

但是,

#include "filename"

# include“文件名”

will find the file in the same directory where your source file is.

将在源文件所在的目录中找到该文件。

In addition,

此外,

#include "path_to_file/filename"

# include“path_to_file /文件名”

will find the file in the directory which you typed in path_to_file.

将在path_to_file中键入的目录中找到该文件。

#28


-3  

To include a predefined library header file , #include<filename> is used whereas to include user defined header file, #include "filename" is relevant.

要包含预定义的库头文件,#include 被使用,而包括用户定义的头文件,#include“filename”是相关的。