简体   繁体   English

CPP | .h文件(C ++)

[英]CPP | .h files (C++)

I was just wondering what the difference between .cpp and .h files is? 我只是想知道.cpp和.h文件之间的区别是什么? What would I use a header file (.h) for and what would I use a cpp file for? 我将使用头文件(.h)以及我将使用cpp文件的内容是什么?

In general, and it really could be a lot less general: 总的来说,它实际上可能不那么普遍:

.h (header) files are for declarations of things that are used many times, and are #include d in other files .h (标题)文件用于多次使用的事物的声明,并且在其他文件中是#include d

.cpp (implementation) files are for everything else, and are almost never #include d .cpp (实现)文件用于其他所有内容,几乎从不#include d

Technically, there is no difference. 从技术上讲,没有区别。 C++ allows you to put your code in any file, with any format, and it should work. C ++允许您将代码放在任何格式的任何文件中,它应该可以工作。

By convention, you put your declarations (basically, that which makes up your API) in the .h files, and are referred to as "headers". 按照惯例,您将声明(基本上是构成API的声明)放在.h文件中,并称为“标题”。 The .cpp files are for the actual "guts" of your code - the implementation details. .cpp文件用于代码的实际“内容” - 实现细节。

Normally, you have the header files included with #include by other files in your project (and other projects, if you're making a library), so the compiler can get the interface required to compile. 通常,项目中的其他文件(以及其他项目,如果您正在创建库)中包含#include包含的头文件,因此编译器可以获得编译所需的接口。 The implementation, in the .cpp files, is typically implemented so there is one .cpp file "filling in" the implementation per .h file. 通常实现.cpp文件中的实现,因此有一个.cpp文件“填写”每个.h文件的实现。

By convention, .h files is something that you #include. 按照惯例,.h文件是你#include的东西。 CPP files are something you add to your project for compiling into separate object file, and then passing to the linker. CPP文件是您添加到项目中的文件,用于编译到单独的目标文件中,然后传递给链接器。

The .h file is called the header file. .h文件称为头文件。 You usually put your interface there (the stuff you want to be public). 你通常把你的界面放在那里(你想要公开的东西)。 The cpp file is where you actually implement your interface. cpp文件是实际实现接口的地方。

First, both are text files that contain code for the C++ compiler or pre-processor. 首先,两者都是包含C ++编译器或预处理器代码的文本文件。 As far as the system is concerned there is no difference. 就系统而言,没有区别。

By convention different file name extensions are used to indicate the content of files. 按照惯例,不同的文件扩展名用于指示文件的内容。 In C programs you tend to see .h and .c files while in C++ .hpp and .cpp serve the same purposes. 在C程序中,您往往会看到.h和.c文件,而在C ++中.hpp和.cpp用于相同的目的。

The first group, .h and .hpp files, called header files, contains mostly non-executing code such as definitions of constants and function prototypes. 第一组.h和.hpp文件称为头文件,主要包含非执行代码,例如常量和函数原型的定义。 They are added to programs via #include directive and used not only by the program or library in question but by other programs or libraries that will make use of them, declaring interface points and contracts defining values. 它们通过#include指令添加到程序中,不仅用于所讨论的程序或库,还用于将使用它们的其他程序或库,声明接口点和定义值的契约。 They are also used to set metadata that may change when compiling for different operating systems. 它们还用于设置在编译不同操作系统时可能会更改的元数据。

The second group, .c and .cpp files, contain the executing parts of the code for the library or program. 第二组.c和.cpp文件包含库或程序代码的执行部分。

Correct me if I'm wrong but, 如果我错了,请纠正我,但是,

When you #include something, it more-or-less inserts the entire included file into the one with the include command; 当#include某事时,它或多或少会将整个包含的文件插入到包含include命令的文件中; that is, when I include, say "macros.h" in "genericTools.cpp", the entire contents of "macros.h" is placed in "genericTools.cpp" at that point. 也就是说,当我在“genericTools.cpp”中包含“macros.h”时,“macros.h”的全部内容都放在“genericTools.cpp”中。 This is why you need to use things like "#pragma once" or other protections, to prevent including the same file twice. 这就是为什么你需要使用“#pragma once”或其他保护措施,以防止包含相同的文件两次。

Of note, templated code needs to be entirely in the file you're going to be including elsewhere. 值得注意的是,模板化代码需要完全位于您将在其他地方包含的文件中。 (I'm unsure of this - can template specializations be ommited from the included files, and linked like a normal function?) (我不确定这个 - 可以从包含的文件中省略模板特化,并像正常函数一样链接吗?)

The .cpp that is the implementation file is our actual program or code. 作为实现文件的.cpp是我们的实际程序或代码。 When we need to use different inbuilt functions in our code, we must include the header file that is .h files. 当我们需要在代码中使用不同的内置函数时,我们必须包含.h文件的头文件。

These .h files contains the actual code of the inbuilt functions that we use hence we can simply call the respective functions. 这些.h文件包含我们使用的内置函数的实际代码,因此我们可以简单地调用各自的函数。

Therefore, while we compile our code we can see more number of lines compiled than what we have actually coded because not only our code is compiled but along with that the (code of the) functions (that are included in .h files) are also compiled. 因此,当我们编译代码时,我们可以看到编译的行数比我们实际编码的行数多,因为不仅我们的代码被编译,而且(函数的代码)(包括在.h文件中)也是编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM