tool to check multiple inclusion of header file

Discussion in 'C++' started by techBoy, Mar 14, 2006.

  1. techBoy

    techBoy Guest

    I am looking for a tool that can scan my soyrce code and check if a
    header file gets included more then once in a sequece of compiled code.
    Can some one guide me to such a tool !!
     
    techBoy, Mar 14, 2006
    #1
    1. Advertising

  2. techBoy

    Amal P Guest

    Hi,
    Multiple inclusion can be easily avoided by inserting Inclusion
    guards on each headefiles. Follow it while creating any header file.
    Then you need not have to worry about the multiple inclusion problem. I
    will give an example for inclusion guard.

    If your header is somthing like Reader.h, in Reader.h you write,

    #ifndef _READER_H_
    #define _READER_H_

    // Put your complete header code here

    // And at the end you put,
    #endif //_READER_H_ ends here.

    Now there cannot be multiple inclusions. Because when you include
    Reader.h first time "_READER_H_" gets defined and the file gets
    included. But when you try to include it again "_READER_H_" is already
    defined and so the Reader.h will not be included.

    Best regards,
    Amal P.
     
    Amal P, Mar 14, 2006
    #2
    1. Advertising

  3. techBoy

    Gavin Deane Guest

    Amal P wrote:
    > If your header is somthing like Reader.h, in Reader.h you write,
    >
    > #ifndef _READER_H_
    > #define _READER_H_
    >
    > // Put your complete header code here
    >
    > // And at the end you put,
    > #endif //_READER_H_ ends here.


    Just a small nit: names that start with an underscore followed by an
    uppercase letter are reserved for the implementation. You should not
    use them in your own code. So READER_H_ would be a better choice.

    Gavin Deane
     
    Gavin Deane, Mar 14, 2006
    #3
  4. techBoy

    Student Guest

    Hi Gavin,
    Well its not like that. It's just some programming style. You can
    actually use these names. There are a few other places where these kind
    of names are used. Suppose i am building a big library and I want
    others to use it. There I declare names starting with underscore and
    write it to mean that is my system (The whole software as it is). The
    most convenient way that I found out is you keep your own name n number
    for this. So lets say you have <Some-File-Name>.h is the file. So
    please take this name and give
    #ifndef _<Some-file-name><DATE stamp><Time stamp>
    Or something like that.
     
    Student, Mar 14, 2006
    #4
  5. [replying to a statement that names starting with an underscore
    followed by a capital letter...]
    Student wrote:
    > Well its not like that. It's just some programming style.


    False. I can back-up the claim that names starting with an underscore
    followed by a capital letter are reserved in all contexts for the
    standard C++ library implementation quoting the standard:

    17.4.3.1 (lib.reserved.names), paragraph 3:
    If the program declares or defines a name in a context where it
    is reserved, other than as explicitly allowed by this clause, the
    behavior is undefined.

    17.4.3.1.2 (lib.global.names), paragraph 1, first bullet:
    Each name that contains a double underscore ("__") or begins with
    an underscore followed by an uppercase letter (lex.key) is
    reserved to the implementation for any use.

    Your turn!
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
     
    Dietmar Kuehl, Mar 14, 2006
    #5
  6. techBoy

    Gavin Deane Guest

    Please quote some context. From Google Groups, don't use the Reply link
    at the bottom of the message, click on Show Options at the top of the
    message and use the Reply link revealed there. I wrote:

    Just a small nit: names that start with an underscore followed by an
    uppercase letter are reserved for the implementation. You should not
    use them in your own code. So READER_H_ would be a better choice.

    Student wrote:
    > Hi Gavin,
    > Well its not like that.


    It is.

    > It's just some programming style.


    It's not.

    > You can actually use these names.


    You *can*, and you may get away with no problems. I said you *should
    not*. The language standard agrees with me.

    17.4.3.1.2
    Certain sets of names and function signatures are always reserved to
    the implementation:
    - Each name that contains a double underscore (_ _) or begins with an
    underscore followed by an uppercase letter is reserved to the
    implementation for any use.
    - Each name that begins with an underscore is reserved to the
    implementation for use as a name in the global namespace.

    As I said, this is a relatively minor point. But once you know them,
    the rules are very simple and help ensure that you will not have name
    clashes with the implementation.

    > There are a few other places where these kind
    > of names are used. Suppose i am building a big library and I want
    > others to use it. There I declare names starting with underscore and
    > write it to mean that is my system (The whole software as it is).


    What happens if my program uses your library and someone else's library
    too, and you have both used that naming convention? Why not just put
    your library in its own namespace?

    > The most convenient way that I found out is you keep your own name n number
    > for this. So lets say you have <Some-File-Name>.h is the file. So
    > please take this name and give
    > #ifndef _<Some-file-name><DATE stamp><Time stamp>
    > Or something like that.


    Something like that, yes - just without the leading underscore.

    Gavin Deane
     
    Gavin Deane, Mar 14, 2006
    #6
  7. techBoy

    Gavin Deane Guest

    Gavin Deane wrote:
    > Please quote some context. From Google Groups, don't use the Reply link
    > at the bottom of the message, click on Show Options at the top of the
    > message and use the Reply link revealed there. I wrote:
    >
    > Just a small nit: names that start with an underscore followed by an
    > uppercase letter are reserved for the implementation. You should not
    > use them in your own code. So READER_H_ would be a better choice.
    >
    > Student wrote:
    > > You can actually use these names.

    >
    > You *can*, and you may get away with no problems. I said you *should
    > not*. The language standard agrees with me.


    Actually, the standard is stronger than that. See Dietmar Kuehl's post.
    Using reserved names is undefined behaviour 17.4.3.1/3. So as with all
    undefined behaviour, if you choose to rely on it, all bets are off.

    To repeat from my previous post:
    > As I said, this is a relatively minor point. But once you know them,
    > the rules are very simple and help ensure that you will not have name
    > clashes with the implementation.

    .... and that you will avoid undefined behaviour.

    Gavin Deane
     
    Gavin Deane, Mar 14, 2006
    #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. techBoy
    Replies:
    6
    Views:
    354
    Vladimir S. Oka
    Mar 15, 2006
  2. David T. Ashley

    Multiple Inclusion of Standard Header Files

    David T. Ashley, Jan 14, 2007, in forum: C Programming
    Replies:
    7
    Views:
    374
    Serve Laurijssen
    Jan 14, 2007
  3. Replies:
    11
    Views:
    1,274
    Ian Collins
    Aug 5, 2006
  4. Replies:
    0
    Views:
    410
  5. Parag Mahajan
    Replies:
    8
    Views:
    2,794
    Yannick Tremblay
    Mar 31, 2008
Loading...

Share This Page