/* attempt to deal with prototype, bootstrap, jquery conflicts */ /* for dropdown menus */

\$10

### Image of the Day Submit

IOTD | Top Screenshots

## #define doesn't work

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1そら .  Members

Posted 06 May 2012 - 12:13 PM

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

### #2Sik_the_hedgehog  Members

Posted 06 May 2012 - 12:26 PM

#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
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

### #3Álvaro  Members

Posted 06 May 2012 - 05:59 PM

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, 06 May 2012 - 06:27 PM.

### #4gamepopper  Members

Posted 07 May 2012 - 06:13 AM

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

Posted 07 May 2012 - 06:36 AM

@はとぶ:
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.

### #6__SKYe  Members

Posted 12 May 2012 - 04:48 AM

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>

### #7Hodgman  Moderators

Posted 12 May 2012 - 05:03 AM

Are you using precompiled headers (PCH)?

### #8Álvaro  Members

Posted 12 May 2012 - 03:17 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.