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:
- make an extra Header-File and add
#pragma onceat the top - make an extra Lib and add the typical
#ifndef; #define; #endiflines
Do you have some ideas to fix this problem? I don't define 'disp' multiple times. Just in one Header.
The problem is here:
You define
dispin 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:
Boardcomputer.hinfoo.c, thereforefoo.owill contain a variabledisp. The code compile fine.Boardcomputer.hinbar.c, thereforebar.owill contain a variabledisp. The code compile fine.foo.oandbar.oin order to produce an executable, and the linker sees two variablesdisp, one inbar.oand one infoo.o, hence the multiple definition errorYou want this:
Boardcomputer.h
Boardcomputer.c