This error appears when you put global variables in a .h
file which is included by multiple .cpp
files, causing the same global variable to be created in multiple files, which is not allowed.
E.g., my_header.h
#ifndef _MY_HEADER_H
#define _MY_HEADER_H
int myGlobalVariable = 123; // **WRONG**
#endif
When it should be
my_header.h
#ifndef _MY_HEADER_H
#define _MY_HEADER_H
extern int myGlobalVariable; // CORRECT DECLARTION
#endif
And then in some cpp file, e.g., global_vars.cpp
, the global variable needs to be defined once.
#include "my_header.h"
int myGlobalVariable = 123; // CORRECT DEFINITION (ONLY ONE TIME)
#endif
See Multiple definitions of... error | first defined here... Build Failure.
This error can also occur without an intermediate .h file when two .cpp files try to create the same global variable.
file_1.cpp
#include <Arduino.h>
//..
int myGlobalVariable = 123; // first definition of global var
file_2.cpp
#include <Arduino.h>
//..
int myGlobalVariable = 123; // **WRONG**, second definition of global var ==> multiple definition error
When it should be
file_2.cpp
#include <Arduino.h>
//:.
extern int myGlobalVariable; // OK, share same global variable with file_1