Search code examples
carmstm32hardware

"multiple definition of first defined here". STM32, AC6 Studio


Im facing a stupid problem, although everything have to be working fine. My Task is - to initialise I/O registers using datatypes of unions and structures. I have used solely my names for the initialisation, so that i could avoid errors caused by redefinition of system variables. However the compiler still shows me an errors for all my created variables, as they have been initialised elsewhere in project. There is my code for the initialisation of variables:

volatile SYSCFG_t* mysyscfg=(volatile SYSCFG_t*)MYSYSCFG;

volatile RCC_AHB2ENR_t* myrcc_ahb2enr=(volatile RCC_AHB2ENR_t*)(MYRCC|MYAHB2ENR_OFFS);

volatile RCC_APB2ENR_t* myrcc_apb2enr=(volatile RCC_APB2ENR_t*)(MYRCC|MYAPB2ENR_OFFS);

volatile GPIO_t* mygpiob=(volatile GPIO_t*)MYGPIOB;

volatile GPIO_t* mygpioe=(volatile GPIO_t*)MYGPIOE;

volatile GPIO_t* mygpioa=(volatile GPIO_t*)MYGPIOA;

The errrors that i become are the follwing:

src/main.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:262: multiple definition of `mysyscfg'
src/init.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:262: first defined here
src/main.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:263: multiple definition of `myrcc_ahb2enr'
src/init.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:263: first defined here
src/main.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:264: multiple definition of `myrcc_apb2enr'
src/init.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:264: first defined here
src/main.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:265: multiple definition of `mygpiob'
src/init.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:265: first defined here
src/main.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:266: multiple definition of `mygpioe'
src/init.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:266: first defined here
src/main.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:267: multiple definition of `mygpioa'
src/init.o:/home/bogdan/workspace/timers/Debug/../src/lib.h:267: first defined here
collect2: error: ld returned 1 exit status
make: *** [makefile:62: timers.elf] Error 1

Solution

  • Those are linker errors. You are probably doing something like this:

    main.c:

    #include "lib.h"
    

    init.c:

    #include "lib.h"
    

    When the linker inspects the symbols and types in main.c and init.c, it finds that exact same symbols have been defined in each of them, therefore the error. Normally a header files only declare symbols, not define them, but there are some creative uses of conditional code that can get around this:

    lib.h (please think of a more descriptive name for this file)

    #if defined IN_INIT_C
      volatile SYSCFG_t* mysyscfg=(volatile SYSCFG_t*)MYSYSCFG; // Definition
    #else
      extern volatile SYSCFG_T* mysyscfg; // Declaration
    #endif
    

    Now in init.c and only in that file:

    #define IN_INIT_C
    #include "lib.h"
    

    Or you can simply place the definition in init.c and simplify your header by only placing declarations therein.

    You should also read the Q&A's provided by this search:

    https://stackoverflow.com/search?q=%5Bc%5Ddifference+between+declaration+and+definition