C++ .cpp 文件和 .h 文件有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/875479/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:44:28  来源:igfitidea点击:

What is the difference between a .cpp file and a .h file?

c++headerinclude

提问by Macker

Because I've made .cpp files then transfered them into .h files, the only difference I can find is that you can't #include .cpp files. Is there any difference that I am missing?

因为我已经制作了 .cpp 文件,然后将它们传输到 .h 文件中,所以我能找到的唯一区别是你不能 #include .cpp 文件。我缺少什么区别吗?

回答by Macker

The C++ build system (compiler) knows no difference, so it's all one of conventions.

C++ 构建系统(编译器)不知道有什么区别,所以它都是一种约定。

The convention is that .h files are declarations, and .cpp files are definitions.

约定是 .h 文件是声明,而 .cpp 文件是定义。

That's why .h files are #included -- we include the declarations.

这就是 .h 文件被 #included 的原因——我们包括声明。

回答by Klaim

The .cpp file is the compilation unit : it's the real source code file that will be compiled (in C++).

.cpp 文件是编译单元:它是将被编译的真正源代码文件(在 C++ 中)。

The .h (header) files are files that will be virtually copy/pasted in the .cpp files where the #include precompiler instruction appears. Once the headers code is inserted in the .cpp code, the compilation of the .cpp can start.

.h(头文件)文件是将被虚拟复制/粘贴到 #include 预编译器指令出现的 .cpp 文件中的文件。一旦将头代码插入到 .cpp 代码中,就可以开始编译 .cpp。

回答by ChrisW

I know the difference between a declaration and a definition.

我知道声明和定义之间的区别。

Whereas:

然而:

  • A CPP file includes the definitions from any header which it includes (because CPP and header file together become a single 'translation unit')
  • A header file might be included by more than one CPP file
  • The linker typically won't like anything defined in more than one CPP file
  • CPP 文件包括它所包含的任何头文件的定义(因为 CPP 和头文件一起成为一个“翻译单元”)
  • 一个头文件可能包含在多个 CPP 文件中
  • 链接器通常不会喜欢在多个 CPP 文件中定义的任何内容

Therefore any definitions in a header file should be inline or static. Header files also contain declarations which are used by more than one CPP file.

因此,头文件中的任何定义都应该是内联的或静态的。头文件还包含被多个 CPP 文件使用的声明。

Definitions that are neither static nor inline are placed in CPP files. Also, any declarations that are only needed within one CPP file are often placed within that CPP file itself, nstead of in any (sharable) header file.

既非静态也非内联的定义放在 CPP 文件中。此外,仅在一个 CPP 文件中需要的任何声明通常放置在该 CPP 文件本身中,而不是任何(可共享的)头文件中。

回答by Johannes Schaub - litb

A header (.h, .hpp, ...) file contains

标头 ( .h, .hpp, ...) 文件包含

  • Class definitions ( class X { ... };)
  • Inline function definitions ( inline int get_cpus() { ... })
  • Function declarations ( void help();)
  • Object declarations ( extern int debug_enabled;)
  • 类定义 ( class X { ... };)
  • 内联函数定义 ( inline int get_cpus() { ... })
  • 函数声明 ( void help();)
  • 对象声明 ( extern int debug_enabled;)

A source file (.c, .cpp, .cxx) contains

源文件 ( .c, .cpp, .cxx) 包含

  • Function definitions ( void help() { ... }or void X::f() { ... })
  • Object definitions ( int debug_enabled = 1;)
  • 函数定义(void help() { ... }void X::f() { ... }
  • 对象定义 ( int debug_enabled = 1;)

However, the convention that headers are named with a .hsuffix and source files are named with a .cppsuffix is not really required. One can always tell a good compiler how to treat some file, irrespective of its file-name suffix ( -x <file-type>for gcc. Like -x c++).

但是,标题以.h后缀命名而源文件以后缀命名的约定.cpp并不是真正必需的。人们总是可以告诉一个好的编译器如何处理某个文件,而不管它的文件名后缀(-x <file-type>对于 gcc. Like -x c++)。

Source files will contain definitions that must be present only once in the whole program. So if you include a source file somewhere and then link the result of compilation of that file and then the one of the source file itself together, then of course you will get linker errors, because you have those definitions now appear twice: Once in the included source file, and then in the file that included it. That's why you had problems with including the .cppfile.

源文件将包含在整个程序中只能出现一次的定义。因此,如果您在某处包含一个源文件,然后将该文件的编译结果与源文件本身链接在一起,那么您当然会得到链接器错误,因为这些定义现在出现了两次:一次在包含源文件,然后在包含它的文件中。这就是您在包含.cpp文件时遇到问题的原因。

回答by Marc W

.h files, or header files, are used to list the publicly accessible instance variables and and methods in the class declaration. .cpp files, or implementation files, are used to actually implement those methods and use those instance variables.

.h 文件或头文件用于列出类声明中可公开访问的实例变量和方法。.cpp 文件或实现文件用于实际实现这些方法并使用这些实例变量。

The reason they are separate is because .h files aren't compiled into binary code while .cpp files are. Take a library, for example. Say you are the author and you don't want it to be open source. So you distribute the compiled binary library and the header files to your customers. That allows them to easily see all the information about your library's classes they can use without being able to see how you implemented those methods. They are more for the people using your code rather than the compiler. As was said before: it's the convention.

它们分开的原因是因为 .h 文件没有被编译成二进制代码,而 .cpp 文件是。以图书馆为例。假设您是作者并且您不希望它是开源的。因此,您将编译后的二进制库和头文件分发给您的客户。这使他们能够轻松查看有关他们可以使用的库类的所有信息,而无法查看您是如何实现这些方法的。它们更适合使用您的代码而不是编译器的人。正如之前所说:这是惯例。

回答by dj_segfault

A good rule of thumb is ".h files should have declarations [potentially] used by multiple source files, but no code that gets run."

一个好的经验法则是“.h 文件应该有[可能] 被多个源文件使用的声明,但没有可以运行的代码。”

回答by Noldorin

Others have already offered good explanations, but I thought I should clarify the differences between the various extensions:

其他人已经提供了很好的解释,但我认为我应该澄清各种扩展之间的区别:

  Source Files for C: .c
  Header Files for C: .h

  Source Files for C++: .cpp
  Header Files for C++: .hpp

Of course, as it has already been pointed out, these are just conventions. The compiler doesn't actually pay any attention to them - it's purely for the benefit of the coder.

当然,正如已经指出的那样,这些只是惯例。编译器实际上并不关心它们——这纯粹是为了编码器的利益。

回答by bdonlan

By convention, .h files are included by other files, and never compiled directly by themselves. .cpp files are - again, by convention - the roots of the compilation process; they include .h files directly or indirectly, but generally not .cpp files.

按照惯例,.h 文件会被其他文件包含,并且不会被它们自己直接编译。.cpp 文件 - 再次,按照惯例 - 编译过程的根源;它们直接或间接包含 .h 文件,但通常不包含 .cpp 文件。