相比?" />

#include "xxx.h" 的规则是什么?与 #include 相比?

发布于 2024-09-30 20:38:22 字数 102 浏览 3 评论 0 原文

如果我有自己的库项目,我应该使用哪种样式在我的应用程序中 #include 它们的标头?是否有严格的规则,两者对编译器/预处理器实际上有不同的含义还是仅与标准有关?

If I have my own library projects, which style should I use to #include the headers from them in my application? Are there strict rules, and do the two actually have different meanings to the compiler/preprocessor or is it about standards only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哭了丶谁疼 2024-10-07 20:38:22

根据 ISO 标准,规则很少。两种形式都依赖于实现,它们在哪里查找头文件。它们甚至不必是文件。

C++11 的 2.9 节对这两个变体没有区别,只是您可以在 <> 变体中包含 """ 变体中的 > 但很少有人会愚蠢到在文件名中使用这些字符:-)

16.2 节进一步说明:


格式为# include <的预处理指令h-字符序列> new-line 在一系列实现定义的位置中搜索由 <> 分隔符之间的指定序列唯一标识的标头,并导致用标头的全部内容替换该指令。如何指定位置或识别标头是实现定义的。

# include " q-char-sequence" new-line 形式的预处理指令会导致将该指令替换为由 < 之间的指定序列标识的源文件的全部内容。 code>" 分隔符。以实现定义的方式搜索指定的源文件。如果不支持此搜索,或者搜索失败,则重新处理指令,就像读取 # include < ; h-char-sequence> new-line 与原始指令中的相同包含序列(包括 > 字符,如果有)。


我倾向于使用 <> 作为系统标头和 < 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:


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.

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.


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:

Note: Although an implementation may provide a mechanism for making arbitrary source files available to the <> search, in general programmers should use the <> form for headers provided with the implementation, and the "" form for sources outside the control of the implementation.

This isn't mandated but it's a good idea nonetheless.

牵强ㄟ 2024-10-07 20:38:22

通常,通过使用引号,您的意思是头文件位于项目目录的相对位置。另一方面,如果您使用尖括号,编译器会期望您的头文件位置为标准位置。例如 /usr/include/usr/local/include 或编译器的任何其他默认位置。

GCC 中,如果使用 -I 标志,则包含尖括号的内容也会在指定位置进行搜索。

示例:

$ gcc -Wall -I/path/to/my/library/include myfile.c

因此,如果 /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:

$ gcc -Wall -I/path/to/my/library/include myfile.c

So if you have myfile.h in /path/to/my/library/include, you can use #include <myfile.h> in myfile.c source.

月亮是我掰弯的 2024-10-07 20:38:22

在几个不同的操作系统上使用了数十个编译器之后,我的建议是仅对系统和特定于操作的标头包含使用 ,而使用 "yh"其他所有内容,包括您的库和项目标头。

然后,使用编译器的 -I (或其他)选项设置适当的包含搜索路径。如果您使用 makeant 之类的东西来进行构建,这会更容易。

对于第三方软件标头,您可以使用任一形式。如果该软件包已安装并且所有用户都可以访问(例如,在 /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 like make or ant 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.

梦与时光遇 2024-10-07 20:38:22

它影响预处理器搜索包含文件的位置。来自 MSDN:

“引用形式:此形式指示预处理器在包含 #include 语句的文件的同一目录中查找包含文件,然后在包含 (#include) 该文件的任何文件的目录中查找包含文件。预处理器然后沿着 /I 编译器选项指定的路径搜索,然后沿着 INCLUDE 环境变量指定的路径搜索

:此形式指示预处理器首先沿着 /I 编译器选项指定的路径搜索包含文件,然后,当从命令行编译时,沿着 INCLUDE 环境变量指定的路径。”

作为粗略指南,当我尝试指定相对于包含带有 #include 的文件的目录的路径时,我仅使用引号。否则,我只使用尖括号。以我当前项目为例:

#include <algorithm> // standard library headers
#include <numeric>
#include <stack>

#include <boost/function.hpp> // third-party library headers
#include <boost/lexical_cast.hpp>

#include <common/io/util/LineIO.h> // specified relative to my own base include dir
#include "PartitionForest.h" // a header in the current directory

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:

#include <algorithm> // standard library headers
#include <numeric>
#include <stack>

#include <boost/function.hpp> // third-party library headers
#include <boost/lexical_cast.hpp>

#include <common/io/util/LineIO.h> // specified relative to my own base include dir
#include "PartitionForest.h" // a header in the current directory
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文