C++ 如何包含头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3741051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include header files
提问by Andrew Rabon
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.
另一个问题是,(这里来自 Java),我如何访问定义在文件之外的类?说,我有 one.cpp 和 two.cpp。
In one.cpp:
在一个.cpp中:
class Something {
...
In two.cpp:
在两个.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..
像那样?在 Java 中,您只需在类名前加上“public”。在 C++ 中,争论类似乎有点困难。
回答by Greg Hewgill
Angle brackets in #include
directives means the search path is limited to the "system" include directories. Double quotes mean the search path includes the currentdirectory, followed by the system include directories.
#include
指令中的尖括号表示搜索路径仅限于“系统”包含目录。双引号表示搜索路径包含当前目录,后跟系统包含目录。
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.
当您的操作系统使用区分大小写的文件系统时,文件名的大小写很重要。听起来您可能使用的是 Windows 或 Mac OS X,默认情况下文件名不区分大小写。
回答by Delan Azabani
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
.
尖括号在系统头目录(例如/usr/include
)中查找头。Quotes 只是一个绝对或相对路径名,例如/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.
要从其他文件访问类,只需#include
使用该类的另一个文件。确保构建您的程序,以便没有文件被多次包含。
回答by bjskishore123
Question 1
问题 1
Does it matter how I import header files? Does it matter if they're capitalized a certain way as well?
我如何导入头文件有关系吗?如果它们以某种方式大写也有关系吗?
It doesn't matter, but usual practice is,
没关系,但通常的做法是,
- Use angle brackets for system headers.
- User double quotes for User defined headers(Your own headers)
- 对系统标题使用尖括号。
- 用户定义标题的用户双引号(您自己的标题)
Questions 2 & 3
问题 2 & 3
Another question is, (coming from Java here), how do I access a class outside the file it was defined in?
另一个问题是,(这里来自 Java),我如何访问定义在文件之外的类?
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.
您需要将类定义放在头文件中,并在要使用该类的任何位置包含该头文件。对于您的情况,它如下所示。
//One.h
#ifndef ONE_H
#define ONE_H
class Something
{
public:
void doSomething(){}
};
#endif
//Two.cpp
#include "One.h"
class SomethingElse
{
SomeThing *example;
};
回答by David Rodríguez - dribeas
First the simple question:
先问一个简单的问题:
Does it matter if they're capitalized a certain way as well?
如果它们以某种方式大写也有关系吗?
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 mattersin 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 the include
. (All linux and mac oshave case sensitive filesystems by default, in windows you can configure NTFS to be case sensitive also)
在大多数情况下,includes
引用文件,编译器应该能够找到您在系统中包含的文件。因此,在文件系统区分大小写的所有系统中,大小写都很重要。如果您想保持最低限度的可移植性,您应该在文件名和include
. (默认情况下,所有 linux和 mac os都有区分大小写的文件系统,在 Windows 中,您也可以将 NTFS 配置为区分大小写)
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.
现在,文件的命名方式真的很重要吗?不,它不会,只要您在夹杂物中保持一致。另请注意,建议遵循一种模式以简化包含。
Does it matter how I import header files?
我如何导入头文件有关系吗?
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.
到目前为止,标准还不是很清楚,不同的实现遵循不同的路径。该标准定义了它们可能不同,因为编译器搜索包含文件的位置和顺序集是实现定义的,如果包含使用尖括号或双引号,则可能会有所不同。如果包含引号无法定位文件,编译器必须回退以处理包含,就像它是用尖括号编写的一样。
#include <x.h> // search in order in set1 of directories
#include "x.h" // search in order in set2 of directories
// if search fails, search also in set1
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.
这意味着如果文件只存在于 set1 中,两种类型的包含都会找到它。如果文件存在于 set2 但不在 set1 中,则只有引用包含才能找到它。如果 set1 和 set2 中存在具有相同名称的不同文件,则每种包含类型将查找并包含不同的文件。如果在 set1 和 set2 的公共子集中存在两个具有相同名称的文件,但集合的顺序不同,则每个包含类型可以定位不同的文件。
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.
回到现实世界,大多数编译器将只包含 set2 中的当前目录,set1 是所有系统包含位置(通常可以用编译器参数扩展)在这些情况下,如果文件仅存在于当前目录中,#include "a.h"
会找到它,但#include <a.h>
不会。
Now, whether that is the common behavior, there are some impliedsemantics that are idiomatic in C/C++. In general square brackets are used to include system headers and externalheaders, while double quotes are used to include localfiles. There is a grey zone on whether a library in the same project should be considered as localor 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.
现在,无论这是否是常见行为,在 C/C++ 中都有一些惯用的隐含语义。通常方括号用于包含系统头文件和外部头文件,而双引号用于包含本地文件。关于同一个项目中的库应该被视为本地的还是外部的,存在一个灰色地带。也就是说,即使总是用双引号包含会起作用,大多数人还是会使用角引号来引用不属于当前模块的标题。
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 the std::vector
class (or any other standard header). But this is not a practical issue, and I don't think it will ever be.
最后,虽然我知道没有编译器这样做,但标准允许实现(编译器)不将标准头文件作为真实文件生成,而是在内部处理标准头文件的包含。这是理论上#include "vector"
可能无法包含std::vector
类(或任何其他标准头)的定义的唯一情况。但这不是一个实际问题,我认为永远不会。
回答by Component 10
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 herealthough 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.
首先,这#include
是一个 C 预处理器指令,而不是严格意义上的 C++ 语言的一部分。您可以在此处找到有关它的更多信息,尽管这是专门针对 GNU C 预处理器的,因此可能与您使用的有所不同。我认为您应该始终假设包含文件中区分大小写。不这样做可能会使您难以将代码移植到区分大小写的操作系统,例如 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 know where my headers are - I always specify them with
-I
on the compile line. - I've been caught out before when a locally bodged copy of a header overrode the central copy I was hoping to pick up.
- 我知道我的头在哪里 - 我总是
-I
在编译行上指定它们。 - 以前,当标题的本地副本覆盖了我希望获取的中央副本时,我就被发现了。
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)
我还注意到一些副作用,例如使用 make 创建依赖树时(我不太记得这个问题 - 它确实以不同的方式对待不同的包含,遵循一些而不是其他,但这是大约 7 年前)
Secondly, your question about how to reference functions in other files is answered here/
其次,您关于如何在其他文件中引用函数的问题在这里/