c++classredefinitionplatformio

How do I avoid the "multiple definition of ..." Error in this case


Boardcomputer.h:

#ifndef BOARDCOMPUTER_H
#define BOARDCOMPUTER_H

#include <Arduino.h>
#include <TFT_eSPI.h>

TFT_eSPI disp = TFT_eSPI();

...

#endif

Boardcomputer.cpp:

#include <Boardcomputer.h>

^^use disp functions^^
...

Error:

.pio\build\EmmaChip\lib782\libBoardcomputer.a(Boardcomputer.cpp.o):(.bss.disp+0x0): multiple definition of `disp'
.pio\build\EmmaChip\src\main.cpp.o:(.bss.disp+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\EmmaChip\firmware.elf] Error 1

Here is what I tried:

Do you have some ideas to fix this problem? I don't define 'disp' multiple times. Just in one Header.


Solution

  • The problem is here:

    #ifndef BOARDCOMPUTER_H
    #define BOARDCOMPUTER_H
    
    #include <Arduino.h>
    #include <TFT_eSPI.h>
    
    TFT_eSPI disp = TFT_eSPI();    
    ...    
    #endif
    

    You define disp in a header file. Defining variables in header files is plain wrong, it cannot work (except if you include the .h file in a single .c file, but then the .h file is pretty pointless anyway) and it leads to these "multiple definition" issues at link time.

    This happens here:


    You want this:

    Boardcomputer.h

    #ifndef BOARDCOMPUTER_H
    #define BOARDCOMPUTER_H
    
    #include <Arduino.h>
    #include <TFT_eSPI.h>
    
    extern TFT_eSPI disp;     // declare it here
        
    #endif
    

    Boardcomputer.c

    #include "Boardcomputer.h"
    ...
    TFT_eSPI disp = TFT_eSPI();   // define it here
    ...