Project won't compile

Hey there,

i transfered my Project from Arduino IDE to Platformio.

I tried to compile it for Wemos D2 mini but it won’t work …

[04/28/18 17:24:58] Processing d1_mini (platform: espressif8266; board: d1_mini; framework: arduino)
 
Verbose mode can be enabled via `-v, --verbose` option
PLATFORM: Espressif 8266 > WeMos D1 R2 & mini
SYSTEM: ESP8266 80MHz 80KB RAM (4MB Flash)
Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF MODES: FINDER(chain) COMPATIBILITY(light)
Collected 29 compatible libraries
Scanning dependencies...

Dependency Graph
|-- <Wire> v1.0
|-- <HX711> v0.1
|-- <ESP8266_SSD1306> v3.2.7
|   |-- <Wire> v1.0
|-- <ESP8266WiFi> v1.0
|-- <Adafruit MQTT Library> v0.20.1
Linking .pioenvs\d1_mini\firmware.elf
.pioenvs\d1_mini\src\display.cpp.o:(.bss.display+0x0): multiple definition of `display'
.pioenvs\d1_mini\src\data.cpp.o:(.bss.display+0x0): first defined here
.pioenvs\d1_mini\src\display.cpp.o:(.bss.visualBatteryState+0x0): multiple definition of `visualBatteryState'
.pioenvs\d1_mini\src\data.cpp.o:(.bss.visualBatteryState+0x0): first defined here
.pioenvs\d1_mini\src\display.cpp.o:(.bss.contrast+0x0): multiple definition of `contrast'
.pioenvs\d1_mini\src\data.cpp.o:(.bss.contrast+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.data+0x0): multiple definition of `data'
.pioenvs\d1_mini\src\data.cpp.o:(.bss.data+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.display+0x0): multiple definition of `display'
.pioenvs\d1_mini\src\data.cpp.o:(.bss.display+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.datalabel+0x0): multiple definition of `datalabel'
.pioenvs\d1_mini\src\data.cpp.o:(.bss.datalabel+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.GyroZ+0x0): multiple definition of `GyroZ'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.GyroZ+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.GyroY+0x0): multiple definition of `GyroY'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.GyroY+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.GyroX+0x0): multiple definition of `GyroX'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.GyroX+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.Temperature+0x0): multiple definition of `Temperature'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.Temperature+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.AccelZ+0x0): multiple definition of `AccelZ'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.AccelZ+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.AccelY+0x0): multiple definition of `AccelY'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.AccelY+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.AccelX+0x0): multiple definition of `AccelX'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.AccelX+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.contrast+0x0): multiple definition of `contrast'
.pioenvs\d1_mini\src\data.cpp.o:(.bss.contrast+0x0): first defined here
.pioenvs\d1_mini\src\main.cpp.o:(.bss.visualBatteryState+0x0): multiple definition of `visualBatteryState'
.pioenvs\d1_mini\src\data.cpp.o:(.bss.visualBatteryState+0x0): first defined here
.pioenvs\d1_mini\src\sensors.cpp.o:(.bss.GyroZ+0x0): multiple definition of `GyroZ'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.GyroZ+0x0): first defined here
.pioenvs\d1_mini\src\sensors.cpp.o:(.bss.GyroY+0x0): multiple definition of `GyroY'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.GyroY+0x0): first defined here
.pioenvs\d1_mini\src\sensors.cpp.o:(.bss.GyroX+0x0): multiple definition of `GyroX'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.GyroX+0x0): first defined here
.pioenvs\d1_mini\src\sensors.cpp.o:(.bss.Temperature+0x0): multiple definition of `Temperature'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.Temperature+0x0): first defined here
.pioenvs\d1_mini\src\sensors.cpp.o:(.bss.AccelZ+0x0): multiple definition of `AccelZ'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.AccelZ+0x0): first defined here
.pioenvs\d1_mini\src\sensors.cpp.o:(.bss.AccelY+0x0): multiple definition of `AccelY'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.AccelY+0x0): first defined here
.pioenvs\d1_mini\src\sensors.cpp.o:(.bss.AccelX+0x0): multiple definition of `AccelX'
.pioenvs\d1_mini\src\MPU6050.cpp.o:(.bss.AccelX+0x0): first defined here
.pioenvs\d1_mini\src\wireless.cpp.o:(.bss.mqtt+0x0): multiple definition of `mqtt'
.pioenvs\d1_mini\src\main.cpp.o:(.bss.mqtt+0x0): first defined here
.pioenvs\d1_mini\src\wireless.cpp.o:(.bss.espClient+0x0): multiple definition of `espClient'
.pioenvs\d1_mini\src\main.cpp.o:(.bss.espClient+0x0): first defined here
.pioenvs\d1_mini\src\wireless.cpp.o:(.bss.publishData+0x0): multiple definition of `publishData'
.pioenvs\d1_mini\src\main.cpp.o:(.bss.publishData+0x0): first defined here
.pioenvs\d1_mini\src\wireless.cpp.o:(.bss.lastMsg+0x0): multiple definition of `lastMsg'
.pioenvs\d1_mini\src\main.cpp.o:(.bss.lastMsg+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
*** [.pioenvs\d1_mini\firmware.elf] Error 1
 [ERROR] Took 3.27 seconds

Actually the header and cpp files are all fine, so i don’t understand why it thinks it is defined more then once.

All header files are structured the same way, so I’ll give you an example file.

#ifndef wireless_h
#define wireless_h

//Includes Wireless Transmission libaries
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <ESP8266WiFi.h>
#include "configuration.h"

WiFiClient espClient;
Adafruit_MQTT_Client mqtt(&espClient, mqtt_server, 1883);
Adafruit_MQTT_Publish publishData = Adafruit_MQTT_Publish(&mqtt, "/");
long lastMsg = 0;

void MQTT_connect();
void setup_wifi();

#endif

The equivalent cpp file only includes header files and writes out the methods.

I would be glad for any ideas.

This basically the same type of problem which I solved in Getting Error 1 when building [Solved] - #2 by maxgerhardt. WHen you create a new object in a header file, it gets duplicated by every .cpp file that includes it. Thus the error “multiple definition of …” occurs.

To resolve it you must only declare the variables extern in your header files without constructor arguments. Then you define the objects once in only 1 cpp file.

I.e., your wireless.h header would be

#ifndef wireless_h
#define wireless_h

//Includes Wireless Transmission libaries
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <ESP8266WiFi.h>
#include "configuration.h"

extern WiFiClient espClient;
extern Adafruit_MQTT_Client mqtt;
extern Adafruit_MQTT_Publish publishData;
extern long lastMsg;

void MQTT_connect();
void setup_wifi();

#endif

And at the top of wireless.cpp you’d have

//all include fiels [..]

//Define objects once.
WiFiClient espClient;
Adafruit_MQTT_Client mqtt(&espClient, mqtt_server, 1883);
Adafruit_MQTT_Publish publishData = Adafruit_MQTT_Publish(&mqtt, "/");
long lastMsg = 0;

Do that with all the other header files, too.

2 Likes