Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Multiple definitions of a global symbol

BLRCalvin
2008-07-14
2013-03-12
  • BLRCalvin
    BLRCalvin
    2008-07-14

    Hello,

    If a global symbol(variable/function) is defined multiple times in multiple sources, a compiler is expected to throw an error stating "Multiple definitions of a public symbol".

    But SDCC is not reporting any error in such case. The testcase is as below:

    --------- main.c  Begin --------------------------------------------------------
    #include <stdio.h>
    extern void external_func();
    extern int myvar;

    void main(){
      myvar = 3; external_func();
    }
    --------- main.c  End --------------------------------------------------------

    --------- func1.c  Begin --------------------------------------------------------
    #include <stdio.h>
    int myvar = 2;

    void external_func(){
      myvar--;
      while(1){ printf(" %d - external func in func1.c \n\r", myvar); }
    }
    --------- func1.c  End --------------------------------------------------------

    --------- func2.c  Begin --------------------------------------------------------
    #include <stdio.h>
    int myvar = 4;

    void external_func(){
      myvar--;
      while(1){ printf(" %d - external func in func2.c \n\r", myvar); }
    }
    --------- func2.c  End --------------------------------------------------------

    SDCC Version:

    SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.8.0 #5117 (Mar 23 2008) (MINGW32)

    I am using mcs51 port.

    Compiler commands:

    sdcc -mmcs51 main.c
    sdcc -mmcs51 func1.c
    sdcc -mmcs51 func2.c
    sdcc -mmcs51 main.c func1.c func2.c -o output.hex ---> creates hex file successfully without any error/warning corresponding to the multiple definitions of the public symbol.

    Awaiting your reply.

    Regards,
    Raghu.

     
    • BLRCalvin
      BLRCalvin
      2008-07-14

      Sorry,

      There is a typo error in the last command line option.It should be read as:

      sdcc -mmcs51 main.rel func1.rel func2.rel -o output.hex

      Regards,
      Raghu.

       
    • Maarten Brock
      Maarten Brock
      2008-07-18

      This is already registered as a bug. It only happens if the variable is at the same offset within both objects. And it has to do with an exception made for declaring SFR's.

       
  • I am having a similar but slightly different problem with release 2.9.0 writing a multi-module program for the C8051F350.  Several modules include C8051F350.h.  When I link the program, many of the processor’s global symbols, such as: _ADC0F, _PCA0CP0, _PCA0CP1, _ADC0H, _ADC0L, etc., are reported by the linker as having multiple definitions.

    I have seen this problem before, and resolved it, unfortunately I don’t remember how.  Further, I have another project, also with multiple modules, that doesn’t have this problem.  I can see no differences between these projects to explain.  I have carefully examined the compiler, assembler and linker commands and see no differences.

    Thanks,
    Scott

     
  • Hi all,

    I stumbled across the problem: I was including different CPU definitions in some modules that others because the other modules were stolen from a project using a different CPU.  Once I changed the source to all use the same CPU definition file, all is well.

    Scott