C linker error

Discussion in 'C Programming' started by franco ziade, Feb 24, 2005.

  1. franco ziade

    franco ziade Guest

    I have this situation

    file1.h
    { define struct ... B;
    }

    file2.c
    { include file1.h
    make use of B}

    file2.c
    { include file1.h
    make use of B .. }

    It compiles ok however when it tries to link, It gives the famous error
    Struct B : warning, multiply defined symbol

    Can anyone help on what i should be looking for ?
    I don't see anything wrong in the code ..

    Thanks
     
    franco ziade, Feb 24, 2005
    #1
    1. Advertising

  2. In article <Xg9Td.136811$>,
    franco ziade <> wrote:
    :I have this situation

    :file1.h
    :{ define struct ... B;
    :}

    :file2.c
    :{ include file1.h
    :make use of B}

    :file2.c
    :{ include file1.h
    :make use of B .. }

    :It compiles ok however when it tries to link, It gives the famous error
    :Struct B : warning, multiply defined symbol

    If you are linking file2.c in twice to the same executable, it is not
    surprising that you are getting duplicate symbols.

    I will presume here that you mean file1.c in the first inclusion,
    rather than file2.c.


    :Can anyone help on what i should be looking for ?
    :I don't see anything wrong in the code ..

    If file1.h defines storage for B, rather than just declaring a type
    for B, then when you include it in file1.c you are defining that storage
    as being part of file1.o, and when you include file1.h in file2.c
    you are defining that storage as being part of file2.o. Link the two
    together and of course your symbol is multiply defined.

    In file1.h you should only declare the type of B, and possibly have
    an 'extern' reference to it. Then in -one- place, define it's storage.
    --
    Usenet is one of those "Good News/Bad News" comedy routines.
     
    Walter Roberson, Feb 24, 2005
    #2
    1. Advertising

  3. franco ziade

    franco ziade Guest

    I am sorry .. the third file is file3.c
    Now it is corrected below

    "Walter Roberson" <-cnrc.gc.ca> wrote in message
    news:cvja4p$7c9$...
    > In article <Xg9Td.136811$>,
    > franco ziade <> wrote:
    > :I have this situation
    >
    > :file1.h
    > :{ define struct ... B;
    > :}
    >
    > :file2.c
    > :{ include file1.h
    > :make use of B}
    >
    > :file3.c
    > :{ include file1.h
    > :make use of B .. }
    >
    > :It compiles ok however when it tries to link, It gives the famous error
    > :Struct B : warning, multiply defined symbol
    >
    > If you are linking file2.c in twice to the same executable, it is not
    > surprising that you are getting duplicate symbols.
    >
    > I will presume here that you mean file1.c in the first inclusion,
    > rather than file2.c.
    >
    >
    > :Can anyone help on what i should be looking for ?
    > :I don't see anything wrong in the code ..
    >
    > If file1.h defines storage for B, rather than just declaring a type
    > for B, then when you include it in file1.c you are defining that storage
    > as being part of file1.o, and when you include file1.h in file2.c
    > you are defining that storage as being part of file2.o. Link the two
    > together and of course your symbol is multiply defined.
    >
    > In file1.h you should only declare the type of B, and possibly have
    > an 'extern' reference to it. Then in -one- place, define it's storage.
    > --
    > Usenet is one of those "Good News/Bad News" comedy routines.
     
    franco ziade, Feb 24, 2005
    #3
  4. franco ziade

    Jack Klein Guest

    On Wed, 23 Feb 2005 19:31:03 -0500, "franco ziade"
    <> wrote in comp.lang.c:

    > I have this situation
    >
    > file1.h
    > { define struct ... B;
    > }
    >
    > file2.c
    > { include file1.h
    > make use of B}
    >
    > file2.c
    > { include file1.h
    > make use of B .. }
    >
    > It compiles ok however when it tries to link, It gives the famous error
    > Struct B : warning, multiply defined symbol
    >
    > Can anyone help on what i should be looking for ?
    > I don't see anything wrong in the code ..
    >
    > Thanks


    Show the real code from file1.h. All references to the structure
    type. Nobody can tell for sure what your mistake is from the non C
    text that you posted.

    --
    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, Feb 24, 2005
    #4
  5. franco ziade wrote:

    > I am sorry .. the third file is file3.c
    > Now it is corrected below
    >


    But the explanation still is the same:
    If you are _defining_ storage in your header-file and then including
    this headerfile into two different c-files, you are actually defining the
    same stuff two times, once in file2.c and once in file3.c
    (include is a textual include, so everything in file1.h becomes part of
    file2.c AND file3.c)

    >> In file1.h you should only declare the type of B, and possibly have
    >> an 'extern' reference to it. Then in -one- place, define it's storage.


    That's exactly what you have to do to oevrcome the error.

    HTH
    Jogi

    --
    The particular mistake will not be repeated. There are plenty of
    mistakes left that have not yet been used.          A. Tanenbaum  
     
    Jogi Kuenstner, Feb 25, 2005
    #5
    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. Wolfgang Rueckert

    Linker error - unsolved external symbol

    Wolfgang Rueckert, Jun 30, 2003, in forum: C++
    Replies:
    0
    Views:
    718
    Wolfgang Rueckert
    Jun 30, 2003
  2. Martin Magnusson

    Linker error with static const field

    Martin Magnusson, Oct 20, 2003, in forum: C++
    Replies:
    2
    Views:
    446
    Ron Natalie
    Oct 20, 2003
  3. Arne Schmitz
    Replies:
    3
    Views:
    364
    E. Robert Tisdale
    Apr 16, 2004
  4. TR
    Replies:
    3
    Views:
    8,165
    New_user
    Jun 10, 2004
  5. quad
    Replies:
    3
    Views:
    2,338
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Jun 9, 2007
Loading...

Share This Page