Multiple definitions of variable

I am running into an error whenever I try to upload to my ESP32, I get:

consts.h:12: multiple definition of `mqtt_server'; .pio\build\esp32dev\src\connections.cpp.o:C:\Users\<user>\Documents\PlatformIO\Projects\TelTestCheck/src/consts.h:12: first defined here

consts.h

//wifi login 
#pragma once

#define WIFI_NETWORK "Home network"
#define WIFI_PASSWORD "DA552567"

#define WIFI_TIMEOUT_MS 20000

const double stepRotation = 3200.0;


const char* mqtt_server = "127.0.0.1";
const int mqttPort = 1883;

timeUtills.h

#ifndef timeUtils_h
#define timeUtils_h

#include <NTPClient.h>
#include <Arduino.h>
#include "connections.h"

const long  gmtOffset_sec = 3600*5;
const int   daylightOffset_sec = 3600;
const char* ntpServer = "pool.ntp.org";

Connections conn;

class ntpUtils{

    public:  
        void ntpStart();
        void timeKeep();
        void timeIncrement();

};


#endif

connections.h

#ifndef connections_H
#define connections_H 

#include <Arduino.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "consts.h"

void callback(char* topic, byte* payload, unsigned int length);
class Connections{
    String epfemValues[73][4];
    void connectToWiFi();
    void mqttConnect();
    void loopCon();
};

#endif

I have narrowed the problem down to a file called timeUtils.h which contains an include to connections.h, if this include is removed the problem goes away but I do need the connections class. Im not sure how to fix this error

Thanks

you have defined an identifier in a number of files
in header files when you declare an identifier which is defined elsewhere in the progrma use extern
e.g. in const.h

extern const char* mqtt_server;
extern const int mqttPort ;

otherwise the the compiler allocates storage for the variables multiple times and the linker displays multiple definition error messages

As a rule, you shouldn't put the definitions in the .h files, use the .c or .cpp instead

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.