You are not logged in.

#1 2011-04-30 19:36:55

FinallyHere
Member
Registered: 2011-02-27
Posts: 8

[solved] c++ - #ifninclude ?

Hi @ll,

Let's say I have 2 header files, each including "custom.h" which are themselfs included in main.cpp:


header1.h:

#ifndef HEADER1_H
#define HEADER1_H

#include "custom.h"
...
#endif

header2.h:

#ifndef HEADER2_H
#define HEADER2_H

#include "custom.h"
...
#endif

main.cpp:

#include "header1.h"
#include "header2.h"
...

Will the preprocessor include both "custom.h"-includes or just one?

Is there a need for something like an "#ifninclude"-directive?


Thanks in advance 4 your answers!


Edit: bad English

Last edited by FinallyHere (2011-04-30 20:05:27)

Offline

#2 2011-04-30 19:53:37

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [solved] c++ - #ifninclude ?

Try it! You can run your main.cpp through the preprocessor and see the result as simply as:

$ cpp main.cpp

However, I think you're looking for #ifndef rather than #ifndefine

Last edited by falconindy (2011-04-30 19:54:26)

Offline

#3 2011-04-30 19:57:28

FinallyHere
Member
Registered: 2011-02-27
Posts: 8

Re: [solved] c++ - #ifninclude ?

Oh thanks! Sorry about that ifndefine thing, you are correct! I will Edit again smile


Edit:

Output of

$ cpp main.cpp
# 1 "main.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.cpp"
# 1 "header1.h" 1



# 1 "custom.h" 1
# 5 "header1.h" 2
# 2 "main.cpp" 2
# 1 "header2.h" 1
# 3 "main.cpp" 2

int main()
{

  return 0;
}

So i guess that means it is only included once! Thank you, issue solved!

Last edited by FinallyHere (2011-04-30 20:07:02)

Offline

#4 2011-05-01 17:46:27

Zeist
Arch Linux f@h Team Member
Registered: 2008-07-04
Posts: 532

Re: [solved] c++ - #ifninclude ?

Just as something interesting to note:

Both msvc and gcc (and probably a few others) support putting: "#pragma once" at the top of a header file instead of using the #ifndef/#define thing to make sure it is only included once. In both gcc and msvc it is supposed to give minimally faster compiles. Something that you may want to keep in mind for when you are doing large projects, lately I've used it to shave off about 5-10 minutes on a rather long compile at work.

Last edited by Zeist (2011-05-01 17:47:02)


I haven't lost my mind; I have a tape back-up somewhere.
Twitter

Offline

Board footer

Powered by FluxBB