如何包含头文件
如何导入头文件有关系吗?我见过使用双引号和箭头。
#include <stdlib.h>
#include "Some_Header.h"
它们是否以某种方式大写也很重要吗?对此进行试验,似乎都不重要,但我认为教程这样做一定有原因。
另一个问题是,(这里来自 Java),如何访问定义它的文件之外的类?比如说,我有一个.cpp 和两个.cpp。
在 one.cpp 中:
class Something {
...
在 Two.cpp 中:
class SomethingElse {
Something *example;
...
那样?在 Java 中,您只需在类名前面加上“public”即可。在 C++ 中,争论类似乎有点困难。
Does it matter how I import header files? I've seen double quotes as well as arrows used.
#include <stdlib.h>
#include "Some_Header.h"
Does it matter if they're capitalized a certain way as well? Experimenting around with this, it seems neither matters, but I figure there must be a reason for tutorials doing it the way they do.
Another question is, (coming from Java here), how do I access a class outside the file it was defined in? Say, I have one.cpp and two.cpp.
In one.cpp:
class Something {
...
In two.cpp:
class SomethingElse {
Something *example;
...
Like that? In Java you'd just preface a class name with "public." In C++, wrangling classes seems to be a bit more difficult..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
#include
指令中的尖括号表示搜索路径仅限于“系统”包含目录。双引号表示搜索路径包括当前目录,后跟系统包含目录。当您的操作系统使用区分大小写的文件系统时,文件名的大小写很重要。听起来您可能使用的是 Windows 或 Mac OS X,其中文件名默认不区分大小写。
Angle brackets in
#include
directives means the search path is limited to the "system" include directories. Double quotes mean the search path includes the current directory, followed by the system include directories.The case of the filename matters when your OS is using a filesystem that is case sensitive. It sounds like you might be using Windows or Mac OS X, where filenames are case insensitive by default.
尖括号在系统头目录(例如
/usr/include
)中查找头。引号只是绝对或相对路径名,例如/path/to/header.h
或../headers/abc.h
。要从其他文件访问类,只需
#include
包含该类的其他文件即可。请务必构建您的程序,以便不会多次包含任何文件。Angle brackets looks for the header in system header directories (e.g.
/usr/include
). Quotes is just an absolute or relative pathname, such as/path/to/header.h
or../headers/abc.h
.For accessing classes from other files, just
#include
the other file with the class. Be sure to structure your program so that no file is included more than once.首先是简单的问题:
在大多数情况下,
includes
指的是文件,编译器应该能够找到您要包含在系统中的文件。因此,在文件系统区分大小写的所有系统中,大小写都很重要。如果您想保持最低限度的可移植性,则文件名和include
应该保持一致。 (默认情况下,所有 linux和 mac os都有区分大小写的文件系统,在 Windows 中您也可以将 NTFS 配置为区分大小写)现在,文件的命名方式实际上重要吗?不,只要您的内含物一致,就不会。另请注意,建议遵循某种模式以简化包含过程。
到目前为止,该标准还不是很明确,不同的实现遵循不同的路径。标准定义它们可能不同,因为编译器搜索包含文件的位置和顺序集是实现定义的,并且如果包含使用尖括号或双引号,则可能会有所不同。如果用引号包含无法找到文件,则编译器必须回退处理包含,就好像它是用尖括号编写的一样。
这意味着如果文件仅存在于 set1 中,则两种类型的包含都会找到它。如果文件存在于 set2 中但不存在于 set1 中,则只有引用 include 才能找到该文件。如果 set1 和 set2 中存在同名的不同文件,则每种包含类型将查找并包含不同的文件。如果 set1 和 set2 的公共子集中存在两个具有相同名称的文件,但集合的顺序不同,则每个包含类型可以找到不同的文件。
回到现实世界,大多数编译器将仅包含 set2 中的当前目录,而 set1 是所有系统包含位置(通常可以使用编译器参数扩展)在这些情况下,如果文件仅存在于当前目录中,
#include "ah"
会找到它,但#include
不会。现在,无论这是常见行为,还是有一些 C/C++ 中惯用的隐含语义。一般来说,方括号用于包含系统标头和外部标头,而双引号用于包含本地文件。关于同一项目中的库是否应被视为本地或外部存在一个灰色地带。也就是说,即使始终包含双引号是可行的,大多数人也会使用尖引号来引用不属于当前模块的标头。
最后,虽然据我所知没有编译器这样做,但该标准允许实现(编译器)不将标准头生成为真实文件,而是在内部处理标准头的包含。这是理论上
#include "vector"
可能无法包含std::vector
类(或任何其他标准标头)的定义的唯一情况。但这不是一个实际问题,而且我认为它永远不会成为一个实际问题。First the simple question:
In most cases,
includes
refer to files, and the compiler should be able to locate the file that you are including in the system. For that reason capitalization matters in all systems where the filesystem is case sensitive. If you want to keep a minimum of portability you should be consistent in the name of the files and theinclude
. (All linuxand mac oshave case sensitive filesystems by default, in windows you can configure NTFS to be case sensitive also)Now, does it actually matter how the file is named? No, it does not, as long as you are consistent in the inclusions. Also note that it is advisable to follow a pattern to ease the inclusion.
The standard is not really clear to this point, and different implementations followed different paths. The standard defines that they may be different, as the set of locations and order in which the compiler will search for the included file is implementation defined and can differ if the inclusion is with angle brackets or double quotes. If inclusion with quotes fails to locate the file, the compiler must fall back to process the include as if it had been written with angle brackets.
That implies that if a file is only present in set1, both types of includes will locate it. If a file is present in set2 but not set1 only the quote include will locate it. If different files with the same name are present in set1 and set2, then each inclusion type will find and include a different file. If two files with the same name are present in the common subset of set1 and set2, but the ordering of the sets is different each inclusion type can locate a different file.
Back to the real world, most compilers will include only the current directory in set2, with set1 being all the system include locations (which can usually be extended with compiler arguments) In these cases, if a file is only present in the current directory,
#include "a.h"
will locate it, but#include <a.h>
will not.Now, whether that is the common behavior, there are some implied semantics that are idiomatic in C/C++. In general square brackets are used to include system headers and external headers, while double quotes are used to include local files. There is a grey zone on whether a library in the same project should be considered as local or external. That is, even if always including with double quotes will work, most people will use angle quotes to refer to headers that are not part of the current module.
Finally, while no compiler that I know of does it, the standard allows an implementation (compiler) not to produce the standard headers as real files, but process the inclusion of standard headers internally. This is the only case where theoretically
#include "vector"
could fail to include the definitions of thestd::vector
class (or any other standard header). But this is not a practical issue, and I don't think it will ever be.问题1
没关系,但通常的做法是,
标头。
定义的标题(您自己的标题)
问题 2 & 3
您需要将类定义放在头文件中,并在您想要使用该类的任何地方包含该头文件。
对于您的情况,它看起来如下所示。
Question 1
It doesn't matter, but usual practice is,
headers.
defined headers(Your own headers)
Questions 2 & 3
You need to place class definition in a header file and include that header file wherever you want to use the class.
For your case, it would look like below.
首先,#include 是一个 C 预处理器指令,严格来说并不是 C++ 语言的一部分。您可以在此处了解更多信息专门用于 GNU C 预处理器,因此可能与您使用的不同。我认为您应该始终假设包含文件区分大小写。不这样做可能会使您的代码难以移植到区分大小写的操作系统(例如 UNIX)。
使用“”或<>正如上面所解释的,这是相当微妙的,大多数时候你不会注意到任何区别。使用“”通常首先搜索当前目录。我倾向于不使用它:
-I
指定它们。make 创建依赖树时(我不太记得这个问题 - 它确实以不同的方式对待不同的包含,遵循一些而不是其他,但这是大约 7 年前)
我还注意到一些副作用,例如使用 ,关于如何引用其他文件中的函数的问题已得到解答 此处/
Firstly, the
#include
is a C pre-processor directive and not strictly part of the C++ language as such. You can find out more about it here although this is specifically for the GNU C preprocessor so may be different from what you're using. I think you should always assume case-sensitivity in include files. Not doing so might make it difficult to port you code to a case sensitive OS such as UNIX.The use of "" or <> is rather subtle as explained above and most time you will notice no difference. Using "" generally searches the current directory first. I tend not to use this as:
-I
on the compile line.I've also noticed some side effects such as when using make to create dependency trees (I can't quite recall the issue - it did treat the different includes differently, following some and not others, but this was about 7 years ago)
Secondly, your question about how to reference functions in other files is answered here/