• 12
• 27
• 9
• 20
• 33

# #define doesn't work

This topic is 2135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have never experienced this error before, and I am not sure why it happens.

I have narrowed to these files:

main.cpp
 #include <iostream> #define HELLO #include "Header.h" int main() { return 0; } 

 #pragma once #if defined( HELLO ) #define MYINT int #else #define MYINT float #endif 

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

 #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!

##### Share on other sites
#define is only applied to the file that issued it. If you use the #define in main.cpp but not in header.cpp, then the former will have MYINT as an int and the latter will have MYINT as a float.

If you want the definition to apply to all files including header.h then put the #define in header.h! =P

##### Share on other sites
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'. Edited by alvaro

##### Share on other sites

Also changing "#if define(HELLO)" to "#ifdef HELLO" might help.

##### Share on other sites
@???:
You can set defines on the compiler command line. This way they are defined before any file is read. But anyways must be defined for all source files where HELLO must be defined.

@gamepopper:
No. In this case its the same.

##### Share on other sites
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>

##### Share on other sites
Are you using precompiled headers (PCH)?

##### Share on other sites

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.