Including headers only once

Discussion in 'C Programming' started by Miguel, Oct 30, 2003.

  1. Miguel

    Miguel Guest

    I have a header file that I want to share among various files, and I use:

    #ifndef UTIL_H_
    #define UTIL_H_

    file contents


    #endif

    But the compiler gives me an error

    What should I do?
    Miguel, Oct 30, 2003
    #1
    1. Advertising

  2. Miguel <> scribbled the following:
    > I have a header file that I want to share among various files, and I use:


    > #ifndef UTIL_H_
    > #define UTIL_H_


    > file contents



    > #endif


    > But the compiler gives me an error


    > What should I do?


    Telling us what the error says would be a start.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    Joona I Palaste, Oct 30, 2003
    #2
    1. Advertising

  3. Miguel

    Eric Guest

    Miguel <> wrote:

    > I have a header file that I want to share among various files, and I use:
    >
    > #ifndef UTIL_H_
    > #define UTIL_H_
    >
    > file contents
    >
    >
    > #endif
    >
    > But the compiler gives me an error
    >
    > What should I do?


    What is the error?


    --
    == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
    "Therefore the considerations of the intelligent always include both
    benefit and harm." - Sun Tzu
    == Insults, like violence, are the last refuge of the incompetent... ===
    Eric, Oct 30, 2003
    #3
  4. Miguel

    Alex Guest

    Miguel <> wrote:
    > I have a header file that I want to share among various files, and I use:


    > #ifndef UTIL_H_
    > #define UTIL_H_


    > file contents



    > #endif


    > But the compiler gives me an error


    The compiler probably doesn't, but the linker might if you
    have concrete definitions in the header.

    For example, if you have a line such as

    int a; /* notice, no extern */

    in the header file, you are creating the int object 'a' with
    external linkage rather than saying that one such object exists
    elsewhere as you would by prefixing your definition with
    'extern' and making it into a declaration.

    Now, when you are including this header file multiple times
    in the same translation unit, there is no problem because
    of your include guards. However, if are attempting to link
    two or more object files which were generated from separate
    translation units, each of which having included it's own copy
    of the header, then you will get a clash because the
    external linkage object is defined more than once.

    Alex
    Alex, Nov 5, 2003
    #4
    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. Replies:
    4
    Views:
    474
    red floyd
    Jan 19, 2007
  2. J Kenneth King
    Replies:
    8
    Views:
    379
    J Kenneth King
    Oct 24, 2008
  3. Gancy
    Replies:
    4
    Views:
    163
    Rasto Levrinc
    Feb 3, 2005
  4. Nu
    Replies:
    11
    Views:
    151
    John Bokma
    Oct 31, 2006
  5. Replies:
    2
    Views:
    1,185
    Michele Dondi
    May 18, 2007
Loading...

Share This Page