Mutiple definition & first defined here error in own libray

Hello folks,

I rewrite some code mess for a firmware flasher over i2c into a library, but I’m getting multiple definition and first defined here errors. The problem source seems to be the firmware.h.

The idea is this struct:
|–main.cpp
|–flasher.h
|–flasher.cpp
|–firmware.h ← location with the firmware

Working with Platiform.io in vscode and an esp32-dev-kit.

flasher.h:

#ifndef FLASHER_H
#define FLASHER_H

#include <Arduino.h>
#include "firmware.h"

#define SDA_PIN 21
#define SCL_PIN 22

//extern byte CHIP_ADDR;
//extern byte CHIP_DATA[50];

class FLASHER
{
public:
    FLASHER(uint8_t sda = SDA_PIN, uint8_t scl = SCL_PIN);
    void init(byte address = CHIP_ADDR, byte *data = CHIP_DATA);

private:
    /* functions and variable */
};

#endif

flasher.cpp:

#include <Arduino.h>
#include "flasher.h"

FLASHER::FLASHER(uint8_t sda, uint8_t scl)
    : _dataPin(sda), _clockPin(scl)
{
  /* Code stuff */
}

void CS43131::init(byte address, byte *data)
{
  /* Code stuff */
}

firmware.h:

byte CHIP_ADDR = 0x00;

byte CHIP_DATA[50] = 0x00, ... , 0x00;

main.cpp:

#include <Arduino.h>
#includer "flasher.h"

#define SDA 21
#define SCL 22

FLASHER flash(SDA, SCL);

void setup()
{
  flash.init();
}

void main()
{
}


The Error:

.pio\build\esp32dev\liba88\libflasher.a(flasher.cpp.o):(.data.flasher_DATA+0x0): multiple definition of `flasher_DATA'
.pio\build\esp32dev\src\main.cpp.o:(.data.flasher_DATA+0x0): first defined here
.pio\build\esp32dev\liba88\libflasher.a(flasher.cpp.o):(.data.flasher_ADDR+0x0): multiple definition of `flasher_ADDR'
.pio\build\esp32dev\src\main.cpp.o:(.data.flasher_ADDR+0x0): first defined here

It isn’t working with extern declartion too…

I don’t think it will work that way, because it isn’t possible to give over the address and firmware by default? I hope some got a tip for me.

Thanks!
fmvoxa

No. Since you have written an array declaration + definition in a header file, and both flasher.cpp and main.cpp include it, both of these .cpp files will want to create a global array called CHIP_DATA and a global variable called CHIP_ADDR, and you thus get the multiple definition error message.

If the variables are just needed in flasher.cpp, move the contents in there.

If the variable is indeed needed, you declare the variables as extern in the header and move the definition of them in the flasher.cpp file.

firmware.h

#ifndef _FIRMWARE_H_
#define _FIRMWARE_H_

#include <Arduino.h> /* byte */
extern byte CHIP_ADDR;
extern byte CHIP_DATA[50];
#endif

Thanks! This was it.

So how I understand it, it isn’t possible to define the address and data(firmware) out of the library as separated file. Except, how I was doing it before, include the firmware.h in main.cpp and used the .init(address, data).