简体   繁体   English

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

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

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.因为我已经制作了 .cpp 文件,然后将它们传输到 .h 文件中,所以我能找到的唯一区别是你不能 #include .cpp 文件。 Is there any difference that I am missing?我缺少什么区别吗?

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 的原因——我们包括声明。

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. .h(头文件)文件是将被虚拟复制/粘贴到 #include 预编译器指令出现的 .cpp 文件中的文件。 Once the headers code is inserted in the .cpp code, the compilation of the .cpp can start.一旦将头代码插入到 .cpp 代码中,就可以开始编译 .cpp。

A header ( .h , .hpp , ...) file contains标头( .h.hpp 、 ...)文件包含

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

A source file ( .c , .cpp , .cxx ) contains源文件( .c.cpp.cxx )包含

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

However, the convention that headers are named with a .h suffix and source files are named with a .cpp suffix is not really required.但是,头文件以.h后缀命名而源文件以.cpp后缀命名的约定并不是真正需要的。 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++ ).人们总是可以告诉一个好的编译器如何处理某个文件,而不管它的文件名后缀( -x <file-type> for 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 .cpp file.这就是您在包含.cpp文件时遇到问题的原因。

.h files, or header files, are used to list the publicly accessible instance variables and and methods in the class declaration. .h 文件或头文件用于列出类声明中可公开访问的实例变量和方法。 .cpp files, or implementation files, are used to actually implement those methods and use those instance variables. .cpp 文件或实现文件用于实际实现这些方法并使用这些实例变量。

The reason they are separate is because .h files aren't compiled into binary code while .cpp files are.它们分开的原因是因为 .h 文件没有被编译成二进制代码,而 .cpp 文件是。 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.正如之前所说:这是惯例。

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') CPP 文件包括它所包含的任何头文件的定义(因为 CPP 和头文件一起成为一个“翻译单元”)
  • A header file might be included by more than one CPP file一个头文件可能包含在多个 CPP 文件中
  • The linker typically won't like anything defined in more than one CPP file链接器通常不会喜欢在多个 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.既非静态也非内联的定义放在 CPP 文件中。 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 文件本身中,而不是任何(可共享的)头文件中。

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

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 convention, .h files are included by other files, and never compiled directly by themselves.按照惯例,.h 文件会被其他文件包含,并且不会被它们自己直接编译。 .cpp files are - again, by convention - the roots of the compilation process; .cpp 文件 - 再次,按照惯例 - 编译过程的根源; they include .h files directly or indirectly, but generally not .cpp files.它们直接或间接包含 .h 文件,但通常不包含 .cpp 文件。

暂无
暂无

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

相关问题 .h(头文件)和.cpp文件有什么区别? - What is the difference between a .h(header file) and a .cpp file? 在.h 文件中的 class 中声明 static 变量和在 .cpp 文件中声明“全局”变量有什么区别 - What is the difference between declaring a static variable in a class in the .h file and a “global” variable in the .cpp file 在.h文件或.cpp文件中实现类之间的区别 - Difference between implementing a class inside a .h file or in a .cpp file 在标头而不是源文件(cpp / h)中声明变量之间有什么区别 - What is the difference between declaring a variable in a header instead of the source file (cpp/h) 全局const初始化以及.h或.cpp文件中的构造函数之间的差异 - Global const initializing and difference between constructor in .h or .cpp file 将头文件(.h)放在另一个文件(.h 或 .cpp)的开头和结尾的区别 - difference between putting head file(.h) at the beginning and end of another file(.h or .cpp) 包含.cpp文件和.h文件(在cpp中具有相同内容)的区别? - Difference in including the .cpp file and .h file (with the same content in cpp)? 包含.c文件和.h文件有什么区别 - What is the difference between including a .c file and a .h file .h 和 .cpp 文件之间的真正区别是什么? - What's the REAL difference between .h and .cpp files? C ++ - CPP和H中定义的内联之间有什么区别 - C++ - What is the difference between inlines defined in CPP and H
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM