#include "xxx.h" 的规则是什么?与 #include 相比?
如果我有自己的库项目,我应该使用哪种样式在我的应用程序中 #include
它们的标头?是否有严格的规则,两者对编译器/预处理器实际上有不同的含义还是仅与标准有关?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据 ISO 标准,规则很少。两种形式都依赖于实现,它们在哪里查找头文件。它们甚至不必是文件。
C++11 的
2.9
节对这两个变体没有区别,只是您可以在<>
变体中包含"
和""
变体中的>
但很少有人会愚蠢到在文件名中使用这些字符:-)第
16.2
节进一步说明:我倾向于使用
<>
作为系统标头和 < code>"" 作为我自己的标题,但这只是个人偏好,我会注意到前面提到的 C++11 文档指出:这不是强制性的,但仍然是一个好主意。
There are few rules, according to the ISO standard. Both forms are implementation-dependent as to where they look for the header files. They don't even have to be files.
Section
2.9
of C++11 makes no distinction between the two varieties other than the fact you can include"
in the<>
variant and>
in the""
variant but few people would be silly enough to use those characters in file names :-)Section
16.2
further states:I tend to use
<>
for system headers and""
for my own headers, but that's personal preference only. I would note that the aforementioned C++11 document states:This isn't mandated but it's a good idea nonetheless.
通常,通过使用引号,您的意思是头文件位于项目目录的相对位置。另一方面,如果您使用尖括号,编译器会期望您的头文件位置为标准位置。例如
/usr/include
、/usr/local/include
或编译器的任何其他默认位置。在 GCC 中,如果使用
-I
标志,则包含尖括号的内容也会在指定位置进行搜索。示例:
因此,如果
/path/to/my/library/include
中有myfile.h
,则可以使用#include< /code> 在
myfile.c
源中。Usually, by using quotes you mean that header files are located in relative positions to your project's directory. If you use angle brackets, on the other hand, compiler would expect your header files locations to be standard locations. Such as
/usr/include
,/usr/local/include
or any other default locations for your compiler.In GCC if you use the
-I
flag, includes with angle bracket would be searched in the specified locations also.Example:
So if you have
myfile.h
in/path/to/my/library/include
, you can use#include <myfile.h>
inmyfile.c
source.在几个不同的操作系统上使用了数十个编译器之后,我的建议是仅对系统和特定于操作的标头包含使用
,而使用"yh"
其他所有内容,包括您的库和项目标头。然后,使用编译器的
-I
(或其他)选项设置适当的包含搜索路径。如果您使用make
或ant
之类的东西来进行构建,这会更容易。对于第三方软件标头,您可以使用任一形式。如果该软件包已安装并且所有用户都可以访问(例如,在
形式可能更正确。如果它安装在本地构建树中,则
/usr/local/bin
或/usr/site/bin
等位置),则"yh"
形式更正确,因为它是在构建过程中控制的。这种组合是最便携的。
After having used dozens of compilers on several different operating systems, my advice is to use
<x.h>
only for system and operating-specific header includes, and"y.h"
for everything else, including your libraries and project headers.Then you set up the appropriate inclusion search paths using your compiler's
-I
(or whatever) option. This is easier if you use something likemake
orant
to do your builds.For third-party software headers, you could go with either form. If the package is installed and accessible to all users (e.g., in somewhere like
/usr/local/bin
or/usr/site/bin
), then the<x.h>
form is probably more correct. If it's installed within your local build tree, then the"y.h"
form is more correct, since it's controlled within your build process.This combination is the most portable.
它影响预处理器搜索包含文件的位置。来自 MSDN:
“引用形式:此形式指示预处理器在包含 #include 语句的文件的同一目录中查找包含文件,然后在包含 (#include) 该文件的任何文件的目录中查找包含文件。预处理器然后沿着 /I 编译器选项指定的路径搜索,然后沿着 INCLUDE 环境变量指定的路径搜索
:此形式指示预处理器首先沿着 /I 编译器选项指定的路径搜索包含文件,然后,当从命令行编译时,沿着 INCLUDE 环境变量指定的路径。”
作为粗略指南,当我尝试指定相对于包含带有 #include 的文件的目录的路径时,我仅使用引号。否则,我只使用尖括号。以我当前项目为例:
It affects where the preprocessor searches for the include file. From MSDN:
"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 any 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, when compiling from the command line, along the path specified by the INCLUDE environment variable."
As a rough guide, I only use quotes when I'm trying to specify a path relative to the directory containing the file with the #include in it. Otherwise, I just use angle brackets. As an example from my current project: