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

时间:2021-02-24 03:14:33

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文件中,我能找到的唯一区别是你不能包含。cpp文件。我错过了什么不同之处吗?

8 个解决方案

#1


45  

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文件包含#——我们包括声明。

#2


19  

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.

头文件是在#include预编译指令出现的.cpp文件中复制/粘贴的文件。在.cpp代码中插入标头代码之后,就可以开始编译.cpp了。

#3


12  

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. 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文件本身中,而不是在任何(可共享的)头文件中。

#4


6  

.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文件是。以图书馆为例。假设你是作者,你不希望它是开源的。因此,您将编译好的二进制库和头文件分发给您的客户。这使得他们可以很容易地看到关于您的库的类的所有信息,而不能够看到您是如何实现这些方法的。它们更适合使用您的代码的人,而不是编译器。如前所述:这是惯例。

#5


5  

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

一个头(。h, .hpp,…)文件包含。

  • Class definitions ( class X { ... }; )
  • 类定义(类X{…},)
  • Inline function definitions ( inline int get_cpus() { ... } )
  • 内联函数定义(内联int get_cpus(){…})
  • Function declarations ( void help(); )
  • 函数声明(void help();)
  • Object declarations ( extern int debug_enabled; )
  • 对象声明(外部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. 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后缀来命名的。不管文件名称后缀是什么(gcc的-x ),一个好的编译器总是可以告诉它如何处理某个文件。像c++ - x)。

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文件时遇到了问题。

#6


1  

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.

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

#7


1  

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

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

#8


0  

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文件包含在其他文件中,而不是直接由它们自己编译。它们直接或间接包含.h文件,但通常不包括。cpp文件。

#1


45  

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文件包含#——我们包括声明。

#2


19  

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.

头文件是在#include预编译指令出现的.cpp文件中复制/粘贴的文件。在.cpp代码中插入标头代码之后,就可以开始编译.cpp了。

#3


12  

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. 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文件本身中,而不是在任何(可共享的)头文件中。

#4


6  

.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文件是。以图书馆为例。假设你是作者,你不希望它是开源的。因此,您将编译好的二进制库和头文件分发给您的客户。这使得他们可以很容易地看到关于您的库的类的所有信息,而不能够看到您是如何实现这些方法的。它们更适合使用您的代码的人,而不是编译器。如前所述:这是惯例。

#5


5  

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

一个头(。h, .hpp,…)文件包含。

  • Class definitions ( class X { ... }; )
  • 类定义(类X{…},)
  • Inline function definitions ( inline int get_cpus() { ... } )
  • 内联函数定义(内联int get_cpus(){…})
  • Function declarations ( void help(); )
  • 函数声明(void help();)
  • Object declarations ( extern int debug_enabled; )
  • 对象声明(外部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. 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后缀来命名的。不管文件名称后缀是什么(gcc的-x ),一个好的编译器总是可以告诉它如何处理某个文件。像c++ - x)。

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文件时遇到了问题。

#6


1  

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.

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

#7


1  

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

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

#8


0  

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文件包含在其他文件中,而不是直接由它们自己编译。它们直接或间接包含.h文件,但通常不包括。cpp文件。