Why error multiple definition

This is the error message:
pio/build/esp32doit-devkit-v1/libcc7/libDisplayName.a(displayName.cpp.o):(.data.sRadioStation+0x0): multiple definition of `sRadioStation’
.pio/build/esp32doit-devkit-v1/src/main.cpp.o:(.data.sRadioStation+0x0): first defined here

But, AFAIK, I never defined that variable twice (or more).
Some help will be most useful, thanks in advance.

Although you did write the definition of that variable only once, you did so in a header file.

The header file radioStats.h is included by displayName.h, which is included by displayName.cpp and main.cpp.

Hence when the Cpp files are compiled, each resulting object file will have the definition of that variable in it, leading to a “multiple definition” error when all object files are linked together to the firmware.elf.

That’s why a variable is never defined in a header file, only declared. The definition of it has to be put in one single .cpp file, so that only that object file which is created from it, and not multiple other files.

You can fix the error by moving the struct definition in a new file, e.g. src/radioStats.cpp, which includes radioStats.h (to be able to reference the struct type). Then, the radioStats.h just needs to declare the existence of that variable with

extern struct RadioStation sRadioStation[MAX_STATIONS];

See e.g. related topics like A new PlatformIO user coding wrong, getting multiple definition & first defined here errors - #2 by maxgerhardt

I see, thank you.
I should have spotted that when I could take a look in the translation file.
Is there a way to do that?

It is possible with objdump but very cumbersome. Usually the linker error and tracing back in which .c or .cpp files a variable definition occurrs is enough to solve it.

Thank you.
You said: " Then, the radioStats.h just needs to declare the existence of that variable with:

extern struct RadioStation sRadioStation[MAX_STATIONS];

It turns out that we don’t even need that global var, just splitting up radioStats in *.h and *.cpp does the job.