difference in including the header files

Discussion in 'C++' started by nrhayyal, Nov 18, 2005.

  1. nrhayyal

    nrhayyal Guest

    Hi c++ Gurus,
    Need your blessing.
    while testing few aspects with respect to header file inclusions, i
    observed few things which i would like to share with you.
    i have a file sqlca.h in which a structure sqlca is declared.

    i included this file as a soft link in /usr/include.
    the soft link is as follows:
    sqlca.h -> /usr/opt/db2_08_01/include64/sqlca.h

    then i wrote a small program as below:

    //test1.cpp
    #include<iostream.h>
    #include<sqlca.h> // normal inclusion
    #include<sqlca.h> // testing for duplicate
    int main()
    {
    return 0;
    }

    with this kind of inclusion , i didnt get any warnings/errors.

    But if i included this file as a soft link in myproject/src/include
    and changed the code as shown below:

    //test2.cpp
    #include<iostream.h>
    #include "sqlca.h" // normal inclusion
    #include "sqlca.h" // testing for duplicate
    int main()
    {
    return 0;
    }


    this gives me a error saying redeclaration of "struct sqlca" in
    test2.cpp at 4 line
    previously declared in
    test2.cpp at 3 line
    But the same doesnt throw any error if i create a soft link in
    /usr/include.

    i am using gcc compiler gcc3.3.2

    why is it that :
    if we include "/usr/include/sqlca.h" twice in our program doesnt throw
    any error message,
    where as if we include "myproject/src/include/sqlca.h" throws error.
    how the compiler interpretes header files of standard header and normal
    headers.
    please help me in understanding this.

    Thanks & regards
    Nagaraj Hayyal
    mobile:+919885534944
    nrhayyal, Nov 18, 2005
    #1
    1. Advertising

  2. nrhayyal wrote:
    > why is it that :
    > if we include "/usr/include/sqlca.h" twice in our program doesnt throw
    > any error message,
    > where as if we include "myproject/src/include/sqlca.h" throws error.
    > how the compiler interpretes header files of standard header and normal
    > headers.


    Typically the header files have "guards" like this :

    #ifndef _THIS_FILE_IS_INCLUDED_
    #define _THIS_FILE_IS_INCLUDED_

    // contents of the header file
    #endif

    So if you include this file once, then _THIS_FILE_IS_INCLUDED_ gets
    defined. So next time if you include the same file, the #ifndef
    directive evaluates to false. As a result, the contents of the header
    file are not copied again.

    This is a typical method to avoid multiple inclusions. You can write
    this to your header and it would stop giving errors.

    Hope this helps.
    Neelesh Bodas, Nov 18, 2005
    #2
    1. Advertising

  3. nrhayyal

    nrhayyal Guest

    hi Neelesh,
    thanks for ur quick response.
    the file which i mentioned contains what you mentioned.
    my question was, if the same file is included in "/usr/include/", then
    i dont get any error message on including the same file twice.
    But if the same file is included in "mypoject/src/include/", then i
    get error message on including the same file twice.
    there is a difference in compiler reading files from /sur/include and
    other user defined header files.
    i wanted to know about that.

    thanks
    Nagaraj Hayyal
    nrhayyal, Nov 18, 2005
    #3
  4. nrhayyal

    Guest

    nrhayyal wrote:
    > hi Neelesh,
    > thanks for ur quick response.
    > the file which i mentioned contains what you mentioned.
    > my question was, if the same file is included in "/usr/include/", then
    > i dont get any error message on including the same file twice.
    > But if the same file is included in "mypoject/src/include/", then i
    > get error message on including the same file twice.
    > there is a difference in compiler reading files from /sur/include and
    > other user defined header files.
    > i wanted to know about that.
    >
    > thanks
    > Nagaraj Hayyal


    Your original question was about a difference between

    #include "file.h"

    and

    #include <file.h>

    Those two are differenct and so could lead to different results, but
    the differences are implementation-defined so you are going to have to
    ask a group specific to your compiler.

    Gavin Deane
    , Nov 18, 2005
    #4
  5. * nrhayyal:
    > Need your blessing.
    > while testing few aspects with respect to header file inclusions, i
    > observed few things which i would like to share with you.
    > i have a file sqlca.h in which a structure sqlca is declared.
    >
    > i included this file as a soft link in /usr/include.
    > the soft link is as follows:
    > sqlca.h -> /usr/opt/db2_08_01/include64/sqlca.h
    >
    > then i wrote a small program as below:
    >
    > //test1.cpp
    > #include<iostream.h>


    <iostream.h> is not a standard C++ header. Use <iostream>. Note the
    difference.


    > #include<sqlca.h> // normal inclusion
    > #include<sqlca.h> // testing for duplicate
    > int main()
    > {
    > return 0;
    > }
    >
    > with this kind of inclusion , i didnt get any warnings/errors.


    That is a compiler-specific result.

    It _seems_ that your compiler automatically refrains from actually
    including a standard header twice in the same compilation unit (that
    would help compilation time by saving disk accesses), and also regards
    all headers in the [/usr/include/] directory as standard headers.



    > But if i included this file as a soft link in myproject/src/include
    > and changed the code as shown below:
    >
    > //test2.cpp
    > #include<iostream.h>
    > #include "sqlca.h" // normal inclusion
    > #include "sqlca.h" // testing for duplicate
    > int main()
    > {
    > return 0;
    > }
    >
    >
    > this gives me a error saying redeclaration of "struct sqlca" in
    > test2.cpp at 4 line
    > previously declared in
    > test2.cpp at 3 line
    > But the same doesnt throw any error if i create a soft link in
    > /usr/include.
    >
    > i am using gcc compiler gcc3.3.2
    >
    > why is it that :
    > if we include "/usr/include/sqlca.h" twice in our program doesnt throw
    > any error message,
    > where as if we include "myproject/src/include/sqlca.h" throws error.


    That's because you haven't used a proper include guard in your header
    file.

    Here's a typical include guard construction:

    #ifndef SQLCA_H
    #define SQLCA_H

    ... contents of header file
    #endif

    A typical newbie error is to add an underscore at the front of the
    include guard symbol, like _SQLCA_H_, which produces a symbol reserved
    for the C++ implementation. Another typical error is to use a double
    underscore somewhere (ditto). And a third, not formally an error but
    Bad Practice, to not use ALL UPPERCASE for the macro name.


    > how the compiler interpretes header files of standard header and normal
    > headers.


    The standard only specifies that "xxx" can add additional searching
    compared to <xxx>; there's nothing about include once only.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 18, 2005
    #5
  6. > A typical newbie error is to add an underscore at the front of the
    > include guard symbol, like _SQLCA_H_, which produces a symbol reserved
    > for the C++ implementation.


    Confused a bit here.
    I am working with g++ 3.4.2 and observe that all the Standard library
    header files have guards with a leading underscore. (eg.
    _GLIBCXX_VECTOR)
    Is this to be considered an error/ bad-practice as well? Or is this
    because these are files in the "standard library" ?
    Neelesh Bodas, Nov 18, 2005
    #6
  7. * Neelesh Bodas:
    > > A typical newbie error is to add an underscore at the front of the
    > > include guard symbol, like _SQLCA_H_, which produces a symbol reserved
    > > for the C++ implementation.

    >
    > Confused a bit here.
    > I am working with g++ 3.4.2 and observe that all the Standard library
    > header files have guards with a leading underscore. (eg.
    > _GLIBCXX_VECTOR)
    > Is this to be considered an error/ bad-practice as well? Or is this
    > because these are files in the "standard library" ?


    It's because they're in the standard library. That's what reserved to
    the implementation means. Namely that the C++ implementation can use
    them, freely, not risking conflict with any names _you_ might define.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 18, 2005
    #7
  8. nrhayyal wrote:
    > my question was, if the same file is included in "/usr/include/", then
    > i dont get any error message on including the same file twice.
    > But if the same file is included in "mypoject/src/include/", then i
    > get error message on including the same file twice.
    > there is a difference in compiler reading files from /sur/include and
    > other user defined header files.
    > i wanted to know about that.


    Where files get included from and how, is implementation-defined. You
    need to ask in the newsgroup that talks about your compiler.

    V
    Victor Bazarov, Nov 18, 2005
    #8
  9. nrhayyal

    Jack Klein Guest

    On 18 Nov 2005 03:58:19 -0800, "Neelesh Bodas"
    <> wrote in comp.lang.c++:

    > nrhayyal wrote:
    > > why is it that :
    > > if we include "/usr/include/sqlca.h" twice in our program doesnt throw
    > > any error message,
    > > where as if we include "myproject/src/include/sqlca.h" throws error.
    > > how the compiler interpretes header files of standard header and normal
    > > headers.

    >
    > Typically the header files have "guards" like this :
    >
    > #ifndef _THIS_FILE_IS_INCLUDED_
    > #define _THIS_FILE_IS_INCLUDED_


    Typically you do not, if you know proper programming in C++. It is
    illegal for you to define a symbol like this in your code -- all
    identifiers beginning with an underscore followed by an upper case
    letter, or containing two consecutive underscores anywhere within
    them, are reserved for the implementation in all contexts.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 19, 2005
    #9
    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. Eternally
    Replies:
    7
    Views:
    404
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    596
    Jack Klein
    Jul 24, 2004
  3. Replies:
    3
    Views:
    1,284
    Walter Roberson
    May 1, 2006
  4. 'Mani
    Replies:
    4
    Views:
    477
    gangs
    Apr 3, 2006
  5. mlt
    Replies:
    2
    Views:
    820
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page