correct way to use .h and #includes?

V

vfunc

In some senarios you want to include a file in two or more branches of
your project, what is the correct way to do this ? Can it be done
without #ifdef, or is that the correct way ?
 
V

Victor Bazarov

In some senarios you want to include a file in two or more branches of
your project, what is the correct way to do this ?

Include it.
Can it be done
without #ifdef, or is that the correct way ?

Which #ifdef? Do you mean double inclusion guards? Put them it, they
don't hurt anybody.

V
 
J

Jonathan Mcdougall

In some senarios you want to include a file in two or more branches of
your project, what is the correct way to do this ? Can it be done without #ifdef,
or is that the correct way ?

Depends on what a "branch" is. You'll have to explain a bit more what
the problem is because right now the answer is "well.. just include
it". We ain't mind readers.


Jonathan
 
C

Cy Edmunds

In some senarios you want to include a file in two or more branches of
your project, what is the correct way to do this ? Can it be done
without #ifdef, or is that the correct way ?

A common way to write a header is:

// yadda.h
#if !defined(YADDA_H)
#define YADDA_H
....
#endif // end of file

It's not that elegant, but the language doesn't really provide a better way.
 
P

Phlip

Cy said:
vfunc wrote:
A common way to write a header is:

// yadda.h
#if !defined(YADDA_H)
#define YADDA_H
...
#endif // end of file

It's not that elegant, but the language doesn't really provide a better
way.

Google "include guards", and ain't this in the FAQ?
 
V

vfunc

"It's not that elegant" which is why I was wondering if there was an
improved method. "include guards", sorry i could not imagine that it
would be called that, like Victor said they don't "hurt anybody" so
what are they protecting, is it the compiler ?
 
V

Victor Bazarov

"It's not that elegant" which is why I was wondering if there was an
improved method. "include guards", sorry i could not imagine that it
would be called that, like Victor said they don't "hurt anybody" so
what are they protecting, is it the compiler ?

http://en.wikipedia.org/wiki/Include_guard
Or google "include guards" (as a phrase, with quotes) and read about
them in other sources.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top