# #define doesn't work

This topic is 2043 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
[CODE]
#include <iostream>
#define HELLO
int main()
{
return 0;
}
[/CODE]

[CODE]
#pragma once
#if defined( HELLO )
#define MYINT int
#else
#define MYINT float
#endif
[/CODE]

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":

[CODE]
void function ( )
{
}
[/CODE]

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 [url="http://en.wikipedia.org/wiki/C_preprocessor"]this[/url] 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
[left]Also changing "#if define(HELLO)" to "#ifdef HELLO" might help.[/left]

##### 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
[quote name='3333360' timestamp='1336819729' post='4939537']
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>
[/quote]

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.

##### Share on other sites

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

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628722
• Total Posts
2984397

• 25
• 11
• 10
• 16
• 14