Here, everything works as expected, since in the main.cpp I have defined HELLO in the Header.h MYINT is defined as "int".

However, as soon as I add a .cpp file to "Header.h":

Header.cpp

#include <Header.h>
void function ( )
{
}

then in the Header.h it does no longer recognize the defined "HELLO" in main.cpp, and defines MYINT as "float".

I thought that it might be because the compiler compiles first Header.cpp than main.cpp, however.. I have seen the order in which it comiples it, and it compiles first main.cpp and then Header.cpp.

So I really have no clue why else could it happen such strange behavior.

Any ideas?

Thanks!

"lots of shoulddas, coulddas, woulddas in the air, thinking about things they shouldda couldda wouldda donne, however all those shoulddas coulddas woulddas ran away when they saw the little did to come"

Sik's description is correct, but perhaps I can explain it in other terms: All .cpp files are compiled independently of each other.

The first thing that happens is that the preprocessor expands all the #include directives as if you inserted the contents of the file in the place where the #include appears. It will also expand macros and that kind of thing. You can read this if you want more details.

So when the compiler compiles main.cpp, it sees `#define HELLO' before the #ifdef; but when it compiles Header.cpp, it doesn't.

Just to complete the picture, the result of each compilation is an object file (typically .o or .obj), which are then combined together into the executable in a step called `linking'.

And, by the way, this is probably just a typo, but in 'Header.cpp' it should be

#include "Hello.h"

and not

#include <Hello.h>

Hmmm... Probably right, but it depends on your setup.

At work we use a very strange environment, where all the includes have to be done with <> because we use a list of directories where our files might be located, and the compiler has to go looking for the file in all those directories (similarly to how the PATH environment variable works). We achieve that by configuring the compiler to look for header files in all our directories and using <>.

Anyway, that's a pretty strange arrangement, so you should probably use "" for your own header files and <> for system headers, as usual.