Connecting Tech Pros Worldwide Help | Site Map

Use of #ifndef in header files

Johs
Guest
 
Posts: n/a
#1: Apr 23 '07
Each time I make a new .h file in eclipse it starts with this:

#ifndef FUNCS_H_
#define FUNCS_H_

// here goes all the code

#endif /*FUNCS_H_*/


where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?
Zeppe
Guest
 
Posts: n/a
#2: Apr 23 '07

re: Use of #ifndef in header files


Johs wrote:
Quote:
Each time I make a new .h file in eclipse it starts with this:
>
#ifndef FUNCS_H_
#define FUNCS_H_
>
// here goes all the code
>
#endif /*FUNCS_H_*/
>
>
where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?
yes, in order to prevent multiple inclusion.

Regards,

Zeppe
fcvcnet
Guest
 
Posts: n/a
#3: Apr 23 '07

re: Use of #ifndef in header files


Johs :
Quote:
Each time I make a new .h file in eclipse it starts with this:
>
#ifndef FUNCS_H_
#define FUNCS_H_
>
// here goes all the code
>
#endif /*FUNCS_H_*/
>
>
where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?
C++ Primer, Fourth Edition
By Stanley B. Lippman, Josée Lajoie, Barbara E. Moo

2.9. Writing Our Own Header Files

Avoiding Multiple Inclusions

--
Thank you very much! :)
Thank this newsgroup very much! :)
Visual Studio 2005 Professional Edition
Windows XP Professional
terminator
Guest
 
Posts: n/a
#4: Apr 23 '07

re: Use of #ifndef in header files


On Apr 23, 1:51 pm, Johs <a...@asd.comwrote:
Quote:
Each time I make a new .h file in eclipse it starts with this:
>
#ifndef FUNCS_H_
#define FUNCS_H_
>
// here goes all the code
>
#endif /*FUNCS_H_*/
>
where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?
It helps the linker a lot.This decreases build time and risk of facing
multiple inclusion related errors.

osmium
Guest
 
Posts: n/a
#5: Apr 23 '07

re: Use of #ifndef in header files


"terminator" wrote:

Quote:
Quote:
>#ifndef FUNCS_H_
>#define FUNCS_H_
>>
>// here goes all the code
>>
>#endif /*FUNCS_H_*/
>>
>where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
>good habit to include these preprocessor lines in each .h file?
>
It helps the linker a lot.This decreases build time and risk of facing
multiple inclusion related errors.
So the linker knows about the include files?


zero
Guest
 
Posts: n/a
#6: Apr 23 '07

re: Use of #ifndef in header files


"osmium" <r124c4u102@comcast.netwrote in news:59444oF2j493pU1
@mid.individual.net:
Quote:
"terminator" wrote:
>
>
Quote:
Quote:
>>#ifndef FUNCS_H_
>>#define FUNCS_H_
>>>
>>// here goes all the code
>>>
>>#endif /*FUNCS_H_*/
>>>
>>where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
>>good habit to include these preprocessor lines in each .h file?
>>
>It helps the linker a lot.This decreases build time and risk of facing
>multiple inclusion related errors.
>
So the linker knows about the include files?
>
and about preprocessor directives?
James Kanze
Guest
 
Posts: n/a
#7: Apr 24 '07

re: Use of #ifndef in header files


On Apr 23, 12:51 pm, Johs <a...@asd.comwrote:
Quote:
Each time I make a new .h file in eclipse it starts with this:
Quote:
#ifndef FUNCS_H_
#define FUNCS_H_
Quote:
// here goes all the code
Quote:
#endif /*FUNCS_H_*/
Quote:
where FUNCS_H_ corresponds to the filename funcs.h. But is it always a
good habit to include these preprocessor lines in each .h file?
It depends. You need some sort of include guard, but such a
simple naming convention runs a great risk of name collision,
e.g. in cases like:

#include "myLib/funcs.h"
#include "yourLib/funcs.h"

Within a project or a single company, you'll probably want to
extend the convention to include the subsystem name as well:

#ifndef CompanyName_myLib_funcs_h
#define CompanyName_myLib_funcs_h
// ...
#endif

For code meant to be used as a third party library, you'll
likely want to go even further; I use something like:

name=` basename "$filename" `
guard1=${prefix}` basename "$filename" | sed -e 's:[^a-zA-
Z0-9_]:_:g' `
guard2=`date +%Y%m%d%H%M%S`
guard3=`od -tx1 -N 16 /dev/random | head -1 | sed -e 's/
^[0-9]* //' -e 's/ //g' | tr '[a-f]' '[A-F]'`
guard=${guard1}_${guard2}${guard3}

echo
echo "#ifndef $guard"
echo "#define $guard"
echo
echo "#endif"
echo "// Local Variables: --- for emacs"
echo "// mode: c++ --- for emacs"
echo "// tab-width: 8 --- for emacs"
echo "// End: --- for emacs"
echo "// vim: set ts=8 sw=4 filetype=cpp: --- for vim"

to generate such headers myself. (I've deleted the code which
generates the copyright, but of course, that will normally be
inserted automatically as well.)

I'm not familiar with eclipse, but I would imagine that you can
configure its editor to generate something along these lines as
well. (The above is part of a Unix shell script, which I've
configured my editor to execute whenever it opens a .hh file
which doesn't already exist.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Closed Thread