Sign in to follow this  

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

If you intended to correct an error in the post then please contact us.

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
#include "Header.h"
int main()
{
return 0;
}
[/CODE]

Header.h
[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":

Header.cpp
[CODE]
#include <Header.h>
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this