#include<文件名>和 #include<文件名>有什么区别? 和#include“文件名”?文件名>文件名>
在 include
指令中使用尖括号和引号有什么区别?
#include <文件名>
#include "文件名"
What is the difference between using angle brackets and quotes in an include
directive?
#include <filename>
#include "filename"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
一般来说,区别在于预处理器搜索头文件的位置:
#include 是包含头文件的预处理器指令。 #include 都用于在程序中添加或包含头文件,但第一个是包含系统头文件,后面一个是用于用户定义的头文件。
检查 gcc 文档 gcc 包含文件< /a>
In general the difference is where the preprocessor searches for the header file:
#include is a preprocessor directive to include header file. Both #include are used to add or include header file in the program, but first is to include system header files and later one for user defined header files.
Check the gcc docs gcc include files
< 之间的字符序列 和> 唯一引用一个标头,它不一定是一个文件。 实现可以按照自己的意愿自由地使用字符序列。 (但是,大多数情况下,只需将其视为文件名并在包含路径中进行搜索,如其他帖子所述。)
如果
#include "file"
形式使用时,实现首先查找给定名称的文件(如果支持)。 如果不支持(支持),或者搜索失败,则实现的行为就像使用其他(#include
)形式一样。此外,还存在第三种形式,当
#include
指令与上述任一形式都不匹配时使用。 在这种形式中,对#include 指令的“操作数”进行了一些基本的预处理(例如宏扩展),并且结果预计与其他两种形式之一匹配。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.)
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.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 <filename>
当你想使用C/C++系统的头文件或编译器库时使用。 这些库可以是 stdio.h、string.h、math.h 等。
#include "path-to-file/filename"
当您想要使用位于项目文件夹或其他位置的自定义头文件时,可以使用这些库。
有关预处理器和标头的更多信息。 阅读C - 预处理器。
#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.
#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.
#include
#include "filename"
#include
并在存储系统头文件的位置搜索该头文件。#include
花费更多的编译时间。#include <filename>
#include "filename"
#include <filename>
and search that header file at where system header files stored.#include <filename>
.理想情况下,您会使用 <...> 对于标准 C 库,“...”对于您编写的并存在于当前目录中的库。
Ideally, you would use <...> for standard C libraries and "..." for libraries that you write and are present in the current directory.
#include
在引用系统文件时使用。 这是一个头文件,可以在系统默认位置(例如/usr/include
或/usr/local/include
)找到。 对于需要包含在另一个程序中的您自己的文件,您必须使用#include "filename"
语法。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.表格 1 - #include < xxx>
首先,在调用指令的当前目录中查找头文件是否存在。 如果未找到,则会在预配置的标准系统目录列表中进行搜索。
形式 2 - #include "xxx"
这会在调用指令的当前目录中查找是否存在头文件。
确切的搜索目录列表取决于目标系统、GCC 的配置方式以及安装位置。
您可以通过使用 -v 选项运行 GCC 编译器来找到其搜索目录列表。
您可以使用 - Idir 将其他目录添加到搜索路径,这会导致在当前目录之后(对于指令的引号形式)和标准系统目录之前搜索 dir。
基本上,“xxx”的形式只不过是在当前目录中搜索; 如果没有发现退回表格
Form 1 - #include < xxx >
First, looks for the presence of header file in the current directory from where directive is invoked. If not found, then it searches in the preconfigured list of standard system directories.
Form 2 - #include "xxx"
This looks for the presence of header file in the current directory from where directive is invoked.
The exact search directory list depends on the target system, how GCC is configured, and where it is installed.
You can find the search directory list of your GCC compiler by running it with -v option.
You can add additional directories to the search path by using - Idir, which causes dir to be searched after the current directory (for the quote form of the directive) and ahead of the standard system directories.
Basically, the form "xxx" is nothing but search in current directory; if not found falling back the form
简单的一般规则是使用尖括号来包含编译器附带的头文件。 使用双引号包含任何其他头文件。 大多数编译器都是这样做的。
1.9 — 头文件详细说明有关预处理器指令的详细信息。 如果您是新手程序员,该页面应该可以帮助您理解所有这些。 我是从这里学到的,并且在工作中也一直遵循着它。
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.
不同之处在于预处理器搜索要包含的文件的位置。
#include <文件名>
预处理器以实现定义的方式进行搜索,通常在编译器/IDE 预先指定的目录中进行搜索。 此方法通常用于包含 C 标准库的头文件以及与目标平台相关的其他头文件。#include“文件名”
预处理器还以实现定义的方式进行搜索,但通常用于包含程序员定义的头文件,并且通常包含与包含指令的文件相同的目录(除非给出绝对路径)。对于 GCC,更完整的描述可以在 GCC 搜索路径文档。
What differs is the locations in which the preprocessor searches for the file to be included.
#include <filename>
The preprocessor searches in an implementation-defined manner, normally in directories pre-designated by the compiler/IDE. This method is normally used to include header files for the C standard library and other header files associated with the target platform.#include "filename"
The preprocessor also searches in an implementation-defined manner, but one that is normally used to include programmer-defined header files and typically includes same directory as the file containing the directive (unless an absolute path is given).For GCC, a more complete description is available in the GCC documentation on search paths.
唯一了解的方法是阅读您的实现文档。
在C 标准中,第 6.10.2 节第 2 至 4 段规定:
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:
按照标准 - 是的,它们是不同的:
请注意,该标准没有说明实现定义的方式之间的任何关系。 第一种形式以一种实现定义的方式进行搜索,另一种形式以(可能是其他)实现定义的方式进行搜索。 该标准还指定应存在某些包含文件(例如,
)。正式来说,您必须阅读编译器的手册,但通常(按照传统)
#include "..."
形式会搜索#include< 所在文件的目录。首先找到/code>,然后是
#include <...>
形式搜索的目录(包含路径,例如系统头文件)。By the standard - yes, they are different:
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).要根据当前配置使用 gcc 查看系统上的搜索顺序,可以执行以下命令。 找到有关此命令的更多详细信息
您可以在此处
cpp - v /dev/null -o /dev/null
To see the search order on your system using gcc, based on current configuration , you can execute the following command. You can find more detail on this command here
cpp -v /dev/null -o /dev/null
#include 语句有两种编写方式。它们是:
每种形式的含义是
该命令将在当前目录以及前面提到的指定目录列表中查找文件
mylib.h
n 可能已设置的包含搜索路径。此命令将仅在指定的目录列表中查找文件
mylib.h
。包含搜索路径只不过是要搜索要包含的文件的目录列表。不同的 C 编译器允许您以不同的方式设置搜索路径。
There exists two ways to write #include statement.These are:
The meaning of each form is
This command would look for the file
mylib.h
in the current directory as well as the specified list of directories as mentioned n the include search path that might have been set up.This command would look for the file
mylib.h
in the specified list of directories only.The include search path is nothing but a list of directories that would be searched for the file being included.Different C compilers let you set the search path in different manners.
编译器生成的实现定义的警告可以(并且将会)以不同于程序库的方式对待系统库。
所以
#include
——实际上声明 myFilename 位于系统库位置——很可能(并且可能会)隐藏死代码和未使用的变量警告等,这些都会显示出来当您使用:
#include "myFilename"
The implementation-defined warnings generated by the compiler can (and will) treat system libraries differently than program libraries.
So
#include <myFilename>
-- which in effect declares that myFilename is in the system library location -- may well (and probably will) hide dead code and unused variable warnings etc, that would show up when you use:
#include "myFilename"
这里的许多答案都集中在编译器将搜索以查找文件的路径。 虽然这是大多数编译器所做的,但允许使用标准头的效果对符合标准的编译器进行预编程,并将
#include
视为一个开关,并且它需要根本不作为文件存在。这并不纯粹是假设。 至少有一个编译器以这种方式工作。 建议仅将
#include
与标准标头一起使用。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.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 <>
用于预定义头文件如果头文件是预定义的,那么您只需将头文件名写在尖括号中,它看起来像这样(假设我们有一个预定义的头文件名称 iostream):
#include " "
用于程序员定义的头文件如果您(程序员)编写了自己的头文件,那么您会将头文件名写在引号中。 因此,假设您编写了一个名为
myfile.h
的头文件,那么这是如何使用 include 指令来包含该文件的示例:#include <>
is for predefined header filesIf 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):
#include " "
is for header files the programmer definesIf 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:示例:
此处的文件名是
Seller.h
:在类实现中(例如
Seller.cpp
),以及将使用文件Seller.cpp 的其他文件中。 h
),现在应该包含用户定义的标头,如下所示:Example:
The filename here is
Seller.h
:In the class implementation (for example,
Seller.cpp
, and in other files that will use the fileSeller.h
), the header defined by the user should now be included, as follows:用于包含标准库文件。 因此编译器将检查标准库头所在的位置。
会告诉编译器包含用户定义的头文件。 因此编译器将在当前文件夹或
-I
定义的文件夹中检查这些头文件。is used to include standard library files. So the compiler will check in the locations where standard library headers are residing.
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.""
将首先搜索./
。 然后搜索默认包含路径。您可以使用这样的命令来打印默认包含路径:
以下是一些示例,可以使事情更清楚:
代码 ac 可以工作,
bc 代码也可以工作,
但是当我在当前目录中创建一个名为
stdio.h
的新文件时ac
会生成编译错误,但是bc< /code> 仍然有效
,并且 "", <> 可以与相同的文件名一起使用。 因为搜索路径优先级不同。
所以
dc
也有效""
will search./
first. Then search the default include path.You can use command like this to print the default include path:
Here are some examples to make thing more clear:
the code a.c works
the code of b.c works too
but when I create a new file named
stdio.h
in current directorya.c
will generate compile error, butb.c
still worksand "", <> can be used together with the same file name. since the search path priority is different.
so
d.c
also works包含默认包含目录所在的文件。
包含编译该文件的当前目录中的文件。 双引号也可以指定到不同位置的完整文件路径。
Includes a file where the default include directory is.
Includes a file in the current directory in which it was compiled. Double quotes can specify a full file path to a different location as well.
在 C++ 中,包含文件有两种方式:
第一种是 #include,它告诉预处理器在预定义的默认位置查找文件。
此位置通常是一个 INCLUDE 环境变量,表示包含文件的路径。
第二种类型是#include“文件名”,它告诉预处理器首先在当前目录中查找文件,然后在用户设置的预定义位置中查找它。
In C++, include a file in two ways:
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.
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 将搜索“依赖于实现的位置列表”(这是表示“系统头”的一种非常复杂的方式)以查找要包含的文件。
带引号的 #include 只会搜索文件(并且“以依赖于实现的方式”,bleh)。 这意味着,用普通英语来说,它将尝试应用您输入的路径/文件名,并且不会在前面添加系统路径或以其他方式篡改它。
另外,如果 #include "" 失败,则会重新读取为 #include <> 按标准。
gcc 文档 有一个(特定于编译器的)描述,尽管特定于gcc 而不是标准,比 ISO 标准的律师式谈话更容易理解。
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.
Also, if #include "" fails, it is re-read as #include <> by the standard.
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.
对于#include "",编译器通常会搜索包含该 include 的文件的文件夹,然后搜索其他文件夹。 对于
#include <>
,编译器不会搜索当前文件的文件夹。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.感谢您的精彩回答,尤其是。 Adam Stelmaszczyk 和 piCookie 以及 aib。
像许多程序员一样,我使用了非正式约定,即对应用程序特定文件使用
"myApp.hpp"
形式,对库和库使用
形式。编译器系统文件,即在/I
和INCLUDE
环境变量中指定的文件,多年来一直认为这是标准。然而,C 标准规定搜索顺序是特定于实现的,这会使可移植性变得复杂。 更糟糕的是,我们使用 jam,它会自动找出包含文件的位置。 您可以对包含文件使用相对或绝对路径。 即
旧版本的 MSVS 需要双反斜杠 (\\),但现在不需要。 我不知道什么时候改变了。 只需使用正斜杠即可与 'nix 兼容(Windows 会接受)。
如果您真的担心它,请使用
“./myHeader.h”
作为与源代码位于同一目录中的包含文件(我当前的非常大的项目有一些重复的包含文件名分散在各处——确实是一个配置管理问题)。为了方便起见,这里复制了 MSDN 说明)。
Thanks for the great answers, esp. Adam Stelmaszczyk and piCookie, and 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 theINCLUDE
environment variable, for years thinking that was the standard.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.
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).
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).Here's the MSDN explanation copied here for your convenience).
至少对于 GCC 版本 <= 3.0,尖括号形式不会在包含文件和包含文件之间生成依赖关系。
因此,如果要生成依赖关系规则(例如使用 GCC -M 选项),则必须对应包含在依赖关系树中的文件使用带引号的形式。
(请参阅http://gcc.gnu.org/onlinedocs/cpp/Initation.html)
At least for GCC version <= 3.0, the angle-bracket form does not generate a dependency between the included file and the including one.
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.
(See http://gcc.gnu.org/onlinedocs/cpp/Invocation.html )
GCC 文档关于两者之间的区别如下:
GCC documentation says the following about the difference between the two:
预处理器的确切行为因编译器而异。 以下答案适用于 GCC 和其他几个编译器。
#include
告诉编译器在其“includes”目录中搜索标头,例如对于 MinGW,编译器将在 C 中搜索file.h
:\MinGW\include\ 或安装编译器的任何位置。#include "file"
告诉编译器在当前目录(即源文件所在的目录)中搜索file
。您可以使用 GCC 的
-I
标志来告诉它,当它遇到带尖括号的包含时,它还应该在-I
之后的目录中搜索标头。 GCC 会将标志后面的目录视为includes
目录。例如,如果您自己的目录中有一个名为
myheader.h
的文件,并且您使用标志 < 调用 GCC,则可以说#include
code>-I . (表示它应该在当前目录中搜索包含内容。)如果没有
-I
标志,您将不得不使用#include "myheader.h "
以包含该文件,或将myheader.h
移动到编译器的include
目录。The exact behavior of the preprocessor varies between compilers. The following answer applies for GCC and several other compilers.
#include <file.h>
tells the compiler to search for the header in its "includes" directory, e.g. for MinGW the compiler would search forfile.h
in C:\MinGW\include\ or wherever your compiler is installed.#include "file"
tells the compiler to search the current directory (i.e. the directory in which the source file resides) forfile
.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
. GCC will treat the directory after the flag as if it were theincludes
directory.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 the flag-I .
(indicating that it should search for includes in the current directory.)Without the
-I
flag, you will have to use#include "myheader.h"
to include the file, or movemyheader.h
to theinclude
directory of your compiler.这里的一些很好的答案引用了 C 标准,但忘记了 POSIX 标准,尤其是 c99 (例如C编译器)命令。
根据The Open Group 基本规范第 7 期,
因此,在符合 POSIX 标准的环境中,使用符合 POSIX 标准的 C 编译器,
#include "file.h"
可能会首先搜索./file.h
,其中.
是带有#include
语句的文件所在的目录,而#include
可能会搜索首先对于/usr/include/file.h
,其中/usr/include
是系统为标头定义的常用位置(似乎没有定义)通过 POSIX)。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.
According to The Open Group Base Specifications Issue 7,
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).
include 告诉预处理器首先在-I
目录和预定义目录中搜索,然后在 .c 文件的目录中搜索。"file"
包含告诉预处理器首先搜索源文件的目录,然后恢复到-I
和预定义。 无论如何,所有目的地都会被搜索,只是搜索顺序不同。2011年标准在“16.2源文件包含”中主要讨论了包含文件。
请注意,如果未找到文件,
"xxx"
形式将降级为
形式。 其余的由实现定义。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.The 2011 standard mostly discusses the include files in "16.2 Source file inclusion".
Note that
"xxx"
form degrades to<xxx>
form if the file is not found. The rest is implementation-defined.它确实:
.
是包含#include
的文件目录,和/或编译器的当前工作目录,和/或 < code>default_include_paths和
如果
./
位于
中,则没有区别。如果 mypath/myfile 位于另一个包含目录中,则行为未定义。
It does:
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 thedefault_include_paths
and
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.