Link Error 'Multiply Define Symbol'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rafi Kfir

    #1

    Link Error 'Multiply Define Symbol'

    "Hi,

    This is a very simple question that confuses me (probably due to some
    lack of knowledge...)

    I will illustrate my problem with a smiple example:


    My project has the following files:


    file1.c
    #include file3.h


    file2.c
    #include file3.h


    file3.h
    #ifndef _file3
    #define _file3


    char a[4]; //this one causes no error
    char b[]="Hello"; //this one causes the error


    #endif


    Can anybody explain why I'm getting the link error upon compilation.


    Thanks"
  • Victor Bazarov

    #2
    Re: Link Error 'Multiply Define Symbol'

    Rafi Kfir wrote:[color=blue]
    > This is a very simple question that confuses me (probably due to some
    > lack of knowledge...)
    >
    > I will illustrate my problem with a smiple example:
    >
    >
    > My project has the following files:
    >
    >
    > file1.c
    > #include file3.h
    >
    >
    > file2.c
    > #include file3.h
    >
    >
    > file3.h
    > #ifndef _file3
    > #define _file3
    >
    >
    > char a[4]; //this one causes no error
    > char b[]="Hello"; //this one causes the error
    >
    >
    > #endif
    >
    >
    > Can anybody explain why I'm getting the link error upon compilation.[/color]

    Because your symbol 'b' is defined in more than one compilation unit.

    Rule: never put definitions of non-const data in headers.

    Hint: double inclusion guards have no effect across compilation units.

    V

    Comment

    • John Harrison

      #3
      Re: Link Error 'Multiply Define Symbol'


      "Rafi Kfir" <rafi.kfir@telr ad.co.il> wrote in message
      news:f74d6851.0 411020739.63ea7 b2d@posting.goo gle.com...[color=blue]
      > "Hi,
      >
      > This is a very simple question that confuses me (probably due to some
      > lack of knowledge...)
      >
      > I will illustrate my problem with a smiple example:
      >
      >
      > My project has the following files:
      >
      >
      > file1.c
      > #include file3.h
      >
      >
      > file2.c
      > #include file3.h
      >
      >
      > file3.h
      > #ifndef _file3
      > #define _file3
      >
      >
      > char a[4]; //this one causes no error[/color]

      It should do
      [color=blue]
      > char b[]="Hello"; //this one causes the error
      >
      >
      > #endif
      >
      >
      > Can anybody explain why I'm getting the link error upon compilation.[/color]

      Do it like this

      // file3.h
      extern char a[];
      extern char b[];

      // either file1.c or file2.c not both
      char a[4]; //this one causes no error
      char b[]="Hello"; //this one causes the error

      You need to understand the difference between a declaration and a
      definition. You can only have one definition. What you had before put the
      definition in the header file. When you include that header file in more
      that one .c file you get multiple definitions and linker errors.

      The way I did it, you have a declaration in the header file, you can have as
      many declarations as you like. But I put the definitions in one of the .c
      files.

      john


      Comment

      • Rafi Kfir

        #4
        Re: Link Error 'Multiply Define Symbol'

        Thanks Victor,
        [color=blue]
        > Hint: double inclusion guards have no effect across compilation units.
        >[/color]

        What is double inclusion guards? Can you explain with an example please?

        Thanks
        Rafi

        Comment

        • Victor Bazarov

          #5
          Re: Link Error 'Multiply Define Symbol'

          Rafi Kfir wrote:[color=blue]
          > Thanks Victor,
          >
          >[color=green]
          >>Hint: double inclusion guards have no effect across compilation units.
          >>[/color]
          >
          >
          > What is double inclusion guards? Can you explain with an example please?[/color]


          It's that stuff that you have in your header in the beginning and at
          the end:

          #ifndef _file3
          #define _file3

          ....

          #endif

          V

          Comment

          Working...