Re: Who owns the variable in my header file ?

Discussion in 'C Programming' started by Ike Naar, Oct 3, 2012.

  1. Ike Naar

    Ike Naar Guest

    On 2012-10-03, lipska the kat <> wrote:
    > I have the following program
    > distributed over 4 files
    >
    > /* foo.h */
    > int foo;
    >
    > void fooset(int f);
    > int fooget(void);
    > void fooinc(void);
    >
    > /* main.c */
    > #include <stdio.h>
    > #include <foo.h>
    >
    > int main(int argc, char **argv){
    >
    > fooset(10);
    > printf("foo is %d\n", fooget());
    >
    > fooinc();
    > printf("foo is now %d\n", fooget());
    >
    > return 0;
    > }
    >
    > /* fooset.c */
    > #include <foo.h>
    >
    > void fooset(int f){
    > extern int foo;


    This extern declaration of 'foo' is harmless but unnecessary since
    the #include <foo.h> already provides such a declaration.

    > foo = f;
    > }
    >
    > /* fooget.c */
    > #include <foo.h>
    >
    > int fooget(void){
    > extern int foo;


    Same here.

    > return foo;
    > }


    What is missing here is fooinc.c, but let's assume it exists
    and contains

    /* fooinc.c */
    #include <foo.h>

    int fooinc(void)
    {
    ++foo;
    }

    > I run make on my makefile (I'm a beginner at make, Ant is more my thing)
    > and see a humungous great glob of bytes called foo.h.gch, looks like
    > foo.h has been compiled ... but I've no idea
    > why it's so huge.
    >
    > -rw-rw-r-- 1 lipska lipska 1339792 Oct 3 17:28 foo.h.gch
    >
    > Anyway, the question is who 'owns' the foo declared in foo.h
    > Storage is obviously set aside as when I run the program I get the
    > expected output
    >
    > foo is 10
    > foo is now 11
    >
    > I guess this big old lump of bytes has something to do with it.


    Actually, the behaviour of your program is undefined, because
    there is more than one external definition for int foo.
    All translation units main.c, fooget.c, fooset.c and fooinc.c
    provide an external definition of int foo. This violates 6.9 p5:

    An external definition is an external declaration that is also a
    definition of a function (other than an inline definition) or an
    object.
    If an identifier declared with external linkage is used in an
    expression (other than as part of the operand of a sizeof or _Alignof
    operator whose result is an integer constant), somewhere in the entire
    program there shall be exactly one external definition for the
    identifier; otherwise, there shall be no more than one.

    Your linker will probably "fix" this, and merge all external
    definitions into one, but if you want your program to be portable
    you should not rely on this linker behaviour.
    It would be cleaner to change the declaration 'int foo;'
    in foo.h by 'extern int foo;', and add a new
    translation unit, say foo.c, to contain the (single)
    definition of int foo:

    /* foo.c */
    #include <foo.h>
    int foo;

    and link the object generated for foo.c to your program
    along with the objects generated for main.c, fooget.c,
    fooset.c and fooinc.c .

    As for the foo.h.gch file, it's a precompiled header,
    the result of compiling foo.h with gcc.
    If you don't want this, don't compile foo.h,
    use it only for inclusion by other *.c files.
    Ike Naar, Oct 3, 2012
    #1
    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. Eric Sosman

    Re: Who owns the variable in my header file ?

    Eric Sosman, Oct 3, 2012, in forum: C Programming
    Replies:
    46
    Views:
    1,080
    Edward A. Falk
    Dec 19, 2012
  2. Kaz Kylheku

    Re: Who owns the variable in my header file ?

    Kaz Kylheku, Oct 3, 2012, in forum: C Programming
    Replies:
    0
    Views:
    370
    Kaz Kylheku
    Oct 3, 2012
  3. Edward A. Falk

    Re: Who owns the variable in my header file ?

    Edward A. Falk, Oct 3, 2012, in forum: C Programming
    Replies:
    5
    Views:
    434
    Keith Thompson
    Oct 11, 2012
  4. James Kuyper

    Re: Who owns the variable in my header file ?

    James Kuyper, Oct 4, 2012, in forum: C Programming
    Replies:
    0
    Views:
    301
    James Kuyper
    Oct 4, 2012
  5. Eric Sosman

    Re: Who owns the variable in my header file ?

    Eric Sosman, Oct 4, 2012, in forum: C Programming
    Replies:
    0
    Views:
    342
    Eric Sosman
    Oct 4, 2012
Loading...

Share This Page