views:

547

answers:

6

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?

+1  A: 

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

Steve Rowe
I concur doctor!this above it too:http://stackoverflow.com/questions/533076/understanding-c-compilers-from-a-java-c-perspective/533201#533201
bobby
A: 

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

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

gbrandt
+2  A: 

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.

Franci Penov
A: 

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.

sharptooth
A: 

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


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.

ChrisW
+1  A: 

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

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 ...
Johannes Schaub - litb
I may be wrong but I think that the class is *declared* in both units and *defined* in only one: the *.h file contains the *declaration* of the class, and the *.cpp contains the *definition*.
ChrisW
no, it contains the definition. what the cpp file contains is the definition of the member functions/static members. but the class definition is placed into the header (a class *declaration* is just "class foo;" while a *definition* could be "class foo { };")
Johannes Schaub - litb