C ++中头文件和cpp文件的约定是什么?

时间:2023-01-13 10:54:29

In C++, what is the convention for including headers for class files in the "main" file. e.g.

在C ++中,在“main”文件中包含类文件头的约定是什么。例如

myclass.h 

class MyClass {
  doSomething();
}


myclass.cpp

  doSomething() {
      cout << "doing something";
  }


run.cpp

#include "myclass.h"
#include "myclass.cpp"

etc..

Is this relatively standard?

这是相对标准的吗?

6 个解决方案

#1


You don't include the .cpp file, only the .h file. The function definitions in the .cpp will be compiled to .obj files, which will then be linked into the final binary. If you include the .cpp file in other .cpp files, you will get two different .obj files with the same funciton definition compiled, which will lead to linker error.

您不包含.cpp文件,仅包含.h文件。 .cpp中的函数定义将被编译为.obj文件,然后将其链接到最终的二进制文件中。如果在其他.cpp文件中包含.cpp文件,则会获得两个编译相同funciton定义的不同.obj文件,这将导致链接器错误。

#2


See Understanding C Compilers for a lot of good answers to this question.

请参阅了解C编译器以获得此问题的许多好答案。

#3


You can say one .cpp file and all its included headers make up one translation unit. As the name implies, one translation unit is compiled on its own. The result, often called file.o or file.obj, of each translation unit, is then linked together by the linker, fixing up yet unresolved references. So in your case you have

您可以说一个.cpp文件及其所有包含的标题组成一个翻译单元。顾名思义,一个翻译单元是自己编译的。然后,每个转换单元的结果(通常称为file.o或file.obj)由链接器链接在一起,修复尚未解析的引用。所以在你的情况下你有

Translation Unit 1 = run.cpp: myclass.h ...
Translation Unit 2 = myclass.cpp: myclass.h ...

You will have your class definition appear in both translation units. But that's OK. It's allowed, as long as both classes are equally defined. But it's not allowed to have the same function appear in the two translation units if the function is not inline. Not inline functions are allowed to be defined only once, in one single translation unit. Then, you have the linker take the result of each translation unit, and bind them together to an executable:

您将在两个翻译单元中显示您的班级定义。但那没关系。只要两个类都被定义相同,就允许这样做。但是如果函数不是内联的,则不允许在两个转换单元中出现相同的函数。不允许在一个单独的翻译单元中仅定义内联函数一次。然后,让链接器获取每个转换单元的结果,并将它们绑定到一个可执行文件:

Executable = mystuff: run.o myclass.o ...

#4


usually you compile the .cpp file separately and link the resulting .o with other .o's

通常你单独编译.cpp文件并将生成的.o链接到其他.o的

So myclass.cpp would include myclass.h and would be compiled as a unit.

所以myclass.cpp将包含myclass.h并将被编译为一个单元。

#5


You compile cpp files separately. If you include any given cpp file into two or more cpp files yoy might encounter a conflict during linking phase.

您可以单独编译cpp文件。如果将任何给定的cpp文件包含到两个或更多cpp文件中,yoy可能会在链接阶段遇到冲突。

#6


You don't include one *.cpp inside another *.cpp. Instead:

您不在另一个* .cpp中包含一个* .cpp。代替:


myclass.h

class MyClass {
  doSomething();
}

myclass.cpp

#include "myclass.h"

MyClass::doSomething() {
      cout << "doing something";
}

run.cpp

#include "myclass.h"

etc..

Instead of including myclass.cpp inside main.cpp (such that the compiler would see both of them in one pass), you compile myclass.cpp and main.cpp separately, and then let the 'linker' combine them into one executable.

不是在main.cpp中包含myclass.cpp(这样编译器会在一次传递中看到它们),而是分别编译myclass.cpp和main.cpp,然后让'链接器'将它们组合成一个可执行文件。

#1


You don't include the .cpp file, only the .h file. The function definitions in the .cpp will be compiled to .obj files, which will then be linked into the final binary. If you include the .cpp file in other .cpp files, you will get two different .obj files with the same funciton definition compiled, which will lead to linker error.

您不包含.cpp文件,仅包含.h文件。 .cpp中的函数定义将被编译为.obj文件,然后将其链接到最终的二进制文件中。如果在其他.cpp文件中包含.cpp文件,则会获得两个编译相同funciton定义的不同.obj文件,这将导致链接器错误。

#2


See Understanding C Compilers for a lot of good answers to this question.

请参阅了解C编译器以获得此问题的许多好答案。

#3


You can say one .cpp file and all its included headers make up one translation unit. As the name implies, one translation unit is compiled on its own. The result, often called file.o or file.obj, of each translation unit, is then linked together by the linker, fixing up yet unresolved references. So in your case you have

您可以说一个.cpp文件及其所有包含的标题组成一个翻译单元。顾名思义,一个翻译单元是自己编译的。然后,每个转换单元的结果(通常称为file.o或file.obj)由链接器链接在一起,修复尚未解析的引用。所以在你的情况下你有

Translation Unit 1 = run.cpp: myclass.h ...
Translation Unit 2 = myclass.cpp: myclass.h ...

You will have your class definition appear in both translation units. But that's OK. It's allowed, as long as both classes are equally defined. But it's not allowed to have the same function appear in the two translation units if the function is not inline. Not inline functions are allowed to be defined only once, in one single translation unit. Then, you have the linker take the result of each translation unit, and bind them together to an executable:

您将在两个翻译单元中显示您的班级定义。但那没关系。只要两个类都被定义相同,就允许这样做。但是如果函数不是内联的,则不允许在两个转换单元中出现相同的函数。不允许在一个单独的翻译单元中仅定义内联函数一次。然后,让链接器获取每个转换单元的结果,并将它们绑定到一个可执行文件:

Executable = mystuff: run.o myclass.o ...

#4


usually you compile the .cpp file separately and link the resulting .o with other .o's

通常你单独编译.cpp文件并将生成的.o链接到其他.o的

So myclass.cpp would include myclass.h and would be compiled as a unit.

所以myclass.cpp将包含myclass.h并将被编译为一个单元。

#5


You compile cpp files separately. If you include any given cpp file into two or more cpp files yoy might encounter a conflict during linking phase.

您可以单独编译cpp文件。如果将任何给定的cpp文件包含到两个或更多cpp文件中,yoy可能会在链接阶段遇到冲突。

#6


You don't include one *.cpp inside another *.cpp. Instead:

您不在另一个* .cpp中包含一个* .cpp。代替:


myclass.h

class MyClass {
  doSomething();
}

myclass.cpp

#include "myclass.h"

MyClass::doSomething() {
      cout << "doing something";
}

run.cpp

#include "myclass.h"

etc..

Instead of including myclass.cpp inside main.cpp (such that the compiler would see both of them in one pass), you compile myclass.cpp and main.cpp separately, and then let the 'linker' combine them into one executable.

不是在main.cpp中包含myclass.cpp(这样编译器会在一次传递中看到它们),而是分别编译myclass.cpp和main.cpp,然后让'链接器'将它们组合成一个可执行文件。