Author Topic: condition dependant compilation error  (Read 2632 times)

Offline Deamon

  • Multiple posting newcomer
  • *
  • Posts: 27
    • Imperial U Flotilla
condition dependant compilation error
« on: April 03, 2006, 07:12:36 am »
Hi forum,

i made a small test app along my c++ study and found an odd circumstance. I made a console project that contains three files with the following test code.
-------------------------------------------------
//main.cpp

#include <iostream>
#include "includestd.h"

#ifndef _process_h
 #include "process.h"
#endif

int main()
{
   std::cout << "Hello world!" << std::endl;
   return 0;
}
--------------------------------------------------
// process.h

class vehicle{int i;};
--------------------------------------------------
// includestd.h

#ifndef _process_h
 #include "process.h"
#endif
--------------------------------------------------

When i try to compile this i get this errors:

process.h:4: redefinition of `class vehicle'
process.h:4: previous definition of `class vehicle'

Obviously this is becose of an double inclusion of the process.h, but why ?

I have included this code in to the main.cpp to prevent a double inclusion but it still includes it double:

#ifndef _process_h
 #include "process.h"
#endif

Do i have a misconception here ?

BTW: When i change the process.h code to:
-------------------------
#ifndef _process_h
#define _process_h

class vehicle{int i;};

#endif
-------------------------

it works fine but shouldn't it work even without it ?

regards,
Deamon
IMPERIAL U-FLOTILLA 1914-1918
http://www.nord-com.net/heinrich.lang/sigs/U1-U31_schr%e4g_600x110.jpg
 Current stage: 1 - Planning

Offline anarxia

  • Single posting newcomer
  • *
  • Posts: 7
Re: condition dependant compilation error
« Reply #1 on: April 03, 2006, 08:31:02 am »
The "standard" way to avoid multiple includes is the 2nd way (that you also confirmed that it works).
Why the other way doesn't work? You never define _process_h!

Offline Deamon

  • Multiple posting newcomer
  • *
  • Posts: 27
    • Imperial U Flotilla
Re: condition dependant compilation error
« Reply #2 on: April 03, 2006, 11:06:32 pm »
Why the other way doesn't work? You never define _process_h!

That means the first method is pointless ? That means that if i have defined the .h then i can include it where ever i want it without fear of an double inclusion in the for of #include "process.h" ?

I don't need to check it via:

#ifndef _process_h
 #include "process.h"
#endif

???

But is there a way to check if a .h was already included and include it only if it's not ?

some sort of:

#ifninc _process_h
 #include "process.h"
#endif

???

regards,
Deamon
« Last Edit: April 03, 2006, 11:29:28 pm by Deamon »
IMPERIAL U-FLOTILLA 1914-1918
http://www.nord-com.net/heinrich.lang/sigs/U1-U31_schr%e4g_600x110.jpg
 Current stage: 1 - Planning

Offline TDragon

  • Lives here!
  • ****
  • Posts: 936
    • Twilight Dragon Media
Re: condition dependant compilation error
« Reply #3 on: April 04, 2006, 12:36:30 am »
Remember, #ifndef / #define / #endif is just a fancy (ab)use of the preprocessor for preventing multiple inclusion; it's not magical in any way.

#ifndef _process_h -- IF "_process_h" has not already been defined
#define _process_h -- then define it now
... (code) ... -- and process the following code
#endif -- (otherwise the intervening code is skipped over, because _process_h was already defined)

If you don't include the line "#define _process_h", then obviously it will always process the code between the #ifndef / #endif, because _process_h is never #defined.

Use "#ifdef" to check if a macro HAS been defined (note the lack of an 'n'). #ifdef and #ifndef are shortened forms of "#if defined" and "#if !defined".
http://tdm-gcc.tdragon.net/ - TDM-GCC compiler suite for Windows (GCC 5.1.0 2015-06-28, 32/64-bit, no extra DLLs)

Offline Deamon

  • Multiple posting newcomer
  • *
  • Posts: 27
    • Imperial U Flotilla
Re: condition dependant compilation error
« Reply #4 on: April 04, 2006, 01:22:43 am »
Ok i think i got it now. Thank you  :)
IMPERIAL U-FLOTILLA 1914-1918
http://www.nord-com.net/heinrich.lang/sigs/U1-U31_schr%e4g_600x110.jpg
 Current stage: 1 - Planning