I've always been curious: in VS, and most other IDEs, you are allowed to do something like this:

//in file class.h
class blah {
   //prototypes, members, blah, blah blah
}
//in file class.cpp
#include "class.h"
//method definitions... etc.

//in file main.cpp
#include "class.h"
int main
{
   //do stuff with the class
   return 0;
}

so, how exactly does that work?
I assumed originally that it was some awesome thing that C++ compilers did, but, after testing with Geany on Ubuntu, I have to include the cpp file, not the header, or it won't find the method definitions. So, is this just based on the way VS builds projects (all files at once), or, is there something it does behind the scenes to make the above code possible?

Recommended Answers

All 4 Replies

I've always been curious: in VS, and most other IDEs, you are allowed to do something like this:

//in file class.h
class blah {
   //prototypes, members, blah, blah blah
}
//in file class.cpp
#include "class.h"
//method definitions... etc.

//in file main.cpp
#include "class.h"
int main
{
   //do stuff with the class
   return 0;
}

so, how exactly does that work?
I assumed originally that it was some awesome thing that C++ compilers did, but, after testing with Geany on Ubuntu, I have to include the cpp file, not the header, or it won't find the method definitions. So, is this just based on the way VS builds projects (all files at once), or, is there something it does behind the scenes to make the above code possible?

It IS something that compilers do. It's not specific to Windows or Visual Studio. You can do it on your own with no IDE. IDE's create a makefile for you, but you can do that on your own, and you don't even need a makefile. Compile into object code first, then link. If you're using g++, the following three commands should do it.

g++ -c class.cpp -o class.o
g++ -c main.cpp -o main.o
g++ main.o class.o -o executableProgram.x

Then run executableProgram.x .

OK, thanks :)

Another, very similar question though, what benefit do I get from including the header, and not the source file?

header file is not executable in its own. It just consists of some declarations and doesn't have the main function whereas the source file has both of them.

OK, thanks :)

Another, very similar question though, what benefit do I get from including the header, and not the source file?

You may not even HAVE the source file. Sometimes the .o and .h files are released, but not the .cpp file. Developers may want other developers to be able to use their class, but not see the source code, so they'll compile the .h and .cpp files into a .o file themselves, then release the .h files and .o files. They may keep the .cpp file to themselves so

  1. You can't steal the source code.
  2. You can't tinker with the source code and screw it up.
  3. The .cpp file is basically a "Black Box". They figure you don't need to know and shouldn't care HOW the class works. Everything you need to know should be in the .h file.

Other times developers release the source code too and thus make it "Open Source" but it's up to the original developer where he/she chooses to do this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.