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

2022-01-25 00:00:00 header include c++

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

myclass.h 

class MyClass {
  doSomething();
}


myclass.cpp

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


run.cpp

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

etc..

Is this relatively standard?

解决方案

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.

相关文章