Problem with include header file

Discussion in 'C Programming' started by chat, May 25, 2006.

  1. chat

    chat Guest

    Hi, every body.

    I have 3 files like this:
    --------------------------------------------------------
    file name : header.h

    #ifndef TEST_H
    #define TEST_H
    int a=1;
    double b=0.5;
    void fn1();
    #endif
    ---------------------------------------------------------
    file name : file1.cpp

    #include<iostream>
    #include "header.h"
    using namespace std;
    void fn1()
    {
    cout<<a;
    }
    ---------------------------------------------------------
    file name : file2.cpp

    #include<iostream>
    #include "header.h"
    using namespace std;
    void main()
    {
    fn1();

    }
    ----------------------------------------------------------

    I use vc++ 6.0 compile all the files and no error occur. The problem
    occur when I build program.
    The error message are:
    file2.obj : error LNK2005: "double b" (?b@@3NA) already defined in
    file1.obj
    file2.obj : error LNK2005: "int a" (?a@@3HA) already defined in
    file1.obj
    Debug/test.exe : fatal error LNK1169: one or more multiply defined
    symbols found

    What is the mistake? Why are variable a,b multiple defined since I use
    compiler directive to assure the multiple include header file?

    Thank you in advance

    Chat
     
    chat, May 25, 2006
    #1
    1. Advertising

  2. chat

    Ico Guest

    chat <> wrote:

    > I have 3 files like this:
    > --------------------------------V------------------------
    > file name : header.h
    >
    > #ifndef TEST_H
    > #define TEST_H
    > int a=1;
    > double b=0.5;
    > void fn1();
    > #endif
    > ---------------------------------------------------------
    > file name : file1.cpp
    >
    > #include<iostream>
    > #include "header.h"
    > using namespace std;
    > void fn1()
    > {
    > cout<<a;
    > }
    > ---------------------------------------------------------
    > file name : file2.cpp
    >
    > #include<iostream>
    > #include "header.h"
    > using namespace std;
    > void main()
    > {
    > fn1();
    >
    > }
    > ----------------------------------------------------------
    >
    > I use vc++ 6.0 compile all the files and no error occur. The problem
    > occur when I build program.
    > The error message are:
    > file2.obj : error LNK2005: "double b" (?b@@3NA) already defined in
    > file1.obj
    > file2.obj : error LNK2005: "int a" (?a@@3HA) already defined in
    > file1.obj
    > Debug/test.exe : fatal error LNK1169: one or more multiply defined
    > symbols found
    >
    > What is the mistake? Why are variable a,b multiple defined since I use
    > compiler directive to assure the multiple include header file?


    Not quite: the #ifdef's only make sure that a single header file is not
    include twice *from the same source*. However, if you include the .h
    from two different .c files, the contents will happily be included, with
    or without #ifdef. Remenber: the compiler only works with one source
    file at a time, and does not know what happend with the previous file,
    or what will happen with the next. At the end, the linker is putting
    everything together, and notices the double declarations.

    After processing by the precompiler, your source files look something
    like this :

    ---------------------------------------------------------
    file name : file1.c

    #include <stdio.h>

    extern int a;
    extern double b;
    void fn1();

    void fn1()
    {
    printf("%d\n", a);
    }

    ---------------------------------------------------------
    file name : file2.cpp

    #include <stdio.h>

    extern int a;
    extern double b;
    void fn1();

    void main()
    {
    fn1();
    }
    ----------------------------------------------------------

    (Note that I changed your code to C, since talking about C++ code on
    comp.lang.c is generally considered Bad Practice)

    As you can see, you are actually declaring int a and double b in both
    source files, so your compiler was right complaining at you.

    Note that declaring variables or functions in header files is generally
    considered Bad Practice as well, for the exact reason you are
    experiencing here. Declare your variables and functions in .c files,
    and only use the header files for the definitions. Try something like
    this :

    ---------------------------------------------------------
    file name : header.h

    #ifndef TEST_H
    #define TEST_H
    extern int a;
    extern double b;
    void fn1();
    #endif
    ---------------------------------------------------------
    file name : file1.c

    #include <stdio.h>
    #include "header.h"

    void fn1()
    {
    printf("%d\n", a);
    }

    ---------------------------------------------------------
    file name : file2.cpp

    #include <stdio.h>
    #include "header.h"

    int a = 2;
    double b = 0.5;

    void main()
    {
    fn1();
    }
    ----------------------------------------------------------



    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    Ico, May 25, 2006
    #2
    1. Advertising

  3. "chat" <> writes:
    > Hi, every body.
    >
    > I have 3 files like this:

    [snip]
    > file name : file1.cpp
    >
    > #include<iostream>
    > #include "header.h"
    > using namespace std;
    > void fn1()
    > {
    > cout<<a;
    > }

    [snip]

    This isn't C.

    comp.lang.c++ is down the hall, past the water cooler, first door on
    the left.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 25, 2006
    #3
  4. chat wrote:
    > Hi, every body.
    >
    > I have 3 files like this:
    > --------------------------------------------------------
    > file name : header.h
    >
    > #ifndef TEST_H
    > #define TEST_H
    > int a=1;
    > double b=0.5;


    having defining declarations (or declarations at all) of variables in a
    header file is a vety bad idea.

    > void fn1();
    > #endif
    > ---------------------------------------------------------
    > file name : file1.cpp
    >
    > #include<iostream>


    And now you have left C entirely. I suspect you wanted
    <news:comp.lang.c++>, where some other language is discussed.
     
    Martin Ambuhl, May 25, 2006
    #4
  5. chat

    chat Guest

    Thank you very much Ico. It helps me a lot. You said "the #ifdef's only
    make sure that a single header file is not include twice *from the
    same source*". I wonder why we need to assure that a single header
    file must be included only one? Because we usually include a single
    header file only one for each source. Or there are another resons?

    Thank you again for your answer.

    Chat
     
    chat, May 25, 2006
    #5
  6. Ico said:

    > void main()


    I know you were merely quoting someone else's code. But of course the return
    type of main is int. People who are not prepared to learn /that/ are
    probably not prepared to learn anything we try to teach them, so I
    recommend that you start off with that.

    Nowadays, if I reply to a void mainer at all, that is likely to be the only
    crit I give for their code. If they can't even be bothered to get the entry
    point right, what's the point?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, May 25, 2006
    #6
  7. chat

    Ian Malone Guest

    chat wrote:
    > Thank you very much Ico. It helps me a lot. You said "the #ifdef's only
    > make sure that a single header file is not include twice *from the
    > same source*". I wonder why we need to assure that a single header
    > file must be included only one? Because we usually include a single
    > header file only one for each source. Or there are another resons?
    >


    First, please see <http://cfaj.freeshell.org/google/>, it helps
    people work out what you're talking about when they can see what
    you're replying to.

    As for your question, the most common use is when you have
    two headers, one which depends on the other.

    a.h:
    struct {
    int a;
    } abc;

    b.h:
    struct abc fooify (struct abc);

    And you have a program which needs to use fooify. You have
    three choices:
    1. Always include a.h then b.h. This is tedious.
    2. Combine a.h and b.h (or make b.h include a.h and never
    include a.h on its own). Fine for the above example,
    and may often make sense, but if a.h represents some
    basic functionality and b.h is a more complex interface
    built on it this doesn't always make sense.
    3. Make b.h include a.h, but use include guards with a.h,
    this way the user of the headers (+libraries) doesn't
    have to worry about including both or just b.h when
    they need b.h, and can include a.h on it's own if they
    just need a.h. Some C implementations use this approach
    to allow standard library headers to be included in any
    order (which the standard requires I believe).

    --
    imalone
     
    Ian Malone, May 25, 2006
    #7
  8. chat

    chat Guest

    Thank you Ian Malone and all other suggestions. It make me more
    understand about what conditional compilation used for and how
    conditional compilation prevent including header file twice.

    Cheer!

    Chat
     
    chat, May 25, 2006
    #8
  9. chat

    pete Guest

    Martin Ambuhl wrote:

    > having defining declarations


    I think you meant "having defintions"

    > (or declarations at all)
    > of variables in a header file is a vety bad idea.


    Declarations of variables with the extern keyword,
    are appropriate in header files.

    --
    pete
     
    pete, May 25, 2006
    #9
  10. chat

    CBFalconer Guest

    chat wrote:
    >
    > I have 3 files like this:
    > --------------------------------------------------------
    > file name : header.h
    >
    > #ifndef TEST_H
    > #define TEST_H
    > int a=1;
    > double b=0.5;
    > void fn1();
    > #endif
    > ---------------------------------------------------------
    > file name : file1.cpp
    >
    > #include<iostream>
    > #include "header.h"
    > using namespace std;
    > void fn1()
    > {
    > cout<<a;
    > }

    .... snip ...
    >
    > What is the mistake? Why are variable a,b multiple defined since
    > I use compiler directive to assure the multiple include header file?


    You have two fundamental problems. First you are declaring data
    objects in header files, which is a no-no. Second, you are using
    C++, which is off-topic here. We deal with C.

    The purpose of headers is to export connections to another source
    file.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, May 25, 2006
    #10
    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. Aguilar, James
    Replies:
    2
    Views:
    726
    Aguilar, James
    Jul 16, 2004
  2. Victor Bazarov

    #include "file" -vs- #include <file>

    Victor Bazarov, Mar 5, 2005, in forum: C++
    Replies:
    4
    Views:
    569
    Exits Funnel
    Mar 6, 2005
  3. PTM
    Replies:
    1
    Views:
    354
    Andy Dingley
    Nov 12, 2007
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    1,006
    Andreas Bogenberger
    Feb 22, 2008
  5. mlt
    Replies:
    2
    Views:
    908
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page