Use of #ifndef in header files

Discussion in 'C++' started by Johs, Apr 23, 2007.

  1. Johs

    Johs Guest

    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?
     
    Johs, Apr 23, 2007
    #1
    1. Advertising

  2. Johs

    Zeppe Guest

    Johs wrote:
    > 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
     
    Zeppe, Apr 23, 2007
    #2
    1. Advertising

  3. Johs

    fcvcnet Guest

    Johs :
    > 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
     
    fcvcnet, Apr 23, 2007
    #3
  4. Johs

    terminator Guest

    On Apr 23, 1:51 pm, Johs <> wrote:
    > 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.
     
    terminator, Apr 23, 2007
    #4
  5. Johs

    osmium Guest

    "terminator" wrote:


    >> #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?
     
    osmium, Apr 23, 2007
    #5
  6. Johs

    zero Guest

    "osmium" <> wrote in news:59444oF2j493pU1
    @mid.individual.net:

    > "terminator" wrote:
    >
    >
    >>> #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?
     
    zero, Apr 23, 2007
    #6
  7. Johs

    James Kanze Guest

    On Apr 23, 12:51 pm, Johs <> wrote:
    > 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 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:
    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
     
    James Kanze, Apr 24, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Evan
    Replies:
    4
    Views:
    919
    Scott Condit
    Jul 12, 2003
  2. Peng Yu
    Replies:
    6
    Views:
    6,511
    Method Man
    Oct 4, 2004
  3. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    639
    Jack Klein
    Jul 24, 2004
  4. Replies:
    3
    Views:
    1,359
    Walter Roberson
    May 1, 2006
  5. mlt
    Replies:
    2
    Views:
    916
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page