Library redeclaration errors

Hello there everyone! This is my first time here and I hope someone can be of help! I'll try to keep it as concise as possible.

So I have two sensors/modules, 1 is an LCD screen from elecrow and another is the DS1307. Both are connected through analog 4 and 5. I tried hooking them up with the help of my breadboard but I've stumbled upon the following issue with the libraries (I don't even know if what I'm trying to achieve is possible but I'm trying nevertheless(and failing horribly while at it))

In file included from C:\Users\c\Documents\Arduino\Sketches\IC2_Checker\BME280_LCD_Time\BME280_LCD_Time.ino:22:0:

C:\Users\c\Documents\Arduino\libraries\RTClib/RTClib.h:79:31: error: redeclaration of 'OFF'

 enum Ds1307SqwPinMode { OFF = 0x00, ON = 0x80, SquareWave1HZ = 0x10, SquareWave4kHz = 0x11, SquareWave8kHz = 0x12, SquareWave32kHz = 0x13 };

                               ^

In file included from C:\Users\c\Documents\Arduino\Sketches\IC2_Checker\BME280_LCD_Time\BME280_LCD_Time.ino:21:0:

C:\Users\c\Documents\Arduino\libraries\I2C_LCD/I2C_LCD.h:457:5: note: previous declaration 'LCD_SwitchState OFF'

     OFF=0x00,

     ^

In file included from C:\Users\c\Documents\Arduino\Sketches\IC2_Checker\BME280_LCD_Time\BME280_LCD_Time.ino:22:0:

C:\Users\c\Documents\Arduino\libraries\RTClib/RTClib.h:79:42: error: redeclaration of 'ON'

 enum Ds1307SqwPinMode { OFF = 0x00, ON = 0x80, SquareWave1HZ = 0x10, SquareWave4kHz = 0x11, SquareWave8kHz = 0x12, SquareWave32kHz = 0x13 };

                                          ^

In file included from C:\Users\c\Documents\Arduino\Sketches\IC2_Checker\BME280_LCD_Time\BME280_LCD_Time.ino:21:0:

C:\Users\c\Documents\Arduino\libraries\I2C_LCD/I2C_LCD.h:458:5: note: previous declaration 'LCD_SwitchState ON'

     ON=0x01

     ^

exit status 1
Error compiling for board Arduino/Genuino Uno.

What I make out of this, is that the libraries are conflicting with each other on the following:

  • OFF = 0x00
  • ON = 0x80
  • ON=0x01

Is there anyway to fix this? I know that I have to edit the .h libraries but I don't exactly know to what they should be changed

Thank you for your time!

C.C

This is a bug in the libraries, and you should probably open an issue to tell them not to add their constants and enums to the global namespace, especially if they have generic names like that.

The easiest way to solve your problem is to just wrap all library code in a namespace.

Another way would be to change "enum" to "enum class". Then use Ds1307SqwPinMode::ON instead of just ON. Note that you have to change that in the library code as well.

Pieter

Thank you for your tip!

I would like to implement your idea but I'm not that confident in my coding. I'm not even sure what a namespace exactly is..

I did however, pinpoint the following:

// RTC based on the DS1307 chip connected via I2C and the Wire library enum Ds1307SqwPinMode { OFF = 0x00, ON = 0x80, SquareWave1HZ = 0x10, SquareWave4kHz = 0x11, SquareWave8kHz = 0x12, SquareWave32kHz = 0x13 };

class RTC_DS1307 { public: boolean begin(void); static void adjust(const DateTime& dt); uint8_t isrunning(void); static DateTime now(); static Ds1307SqwPinMode readSqwPinMode(); static void writeSqwPinMode(Ds1307SqwPinMode mode); uint8_t readnvram(uint8_t address); void readnvram(uint8_t* buf, uint8_t size, uint8_t address); void writenvram(uint8_t address, uint8_t data); void writenvram(uint8_t address, uint8_t* buf, uint8_t size); }; This looks like something which declares the module but I noticed that there's a class called RTC_DS1307. That made me confused as to what I should do to all the other variables in the enum(if they are called like that)

PieterP:
This is a bug in the libraries, and you should probably open an issue to tell them not to add their constants and enums to the global namespace, especially if they have generic names like that.

The easiest way to solve your problem is to just wrap all library code in a namespace.

Another way would be to change “enum” to “enum class”. Then use Ds1307SqwPinMode::ON instead of just ON. Note that you have to change that in the library code as well.

Pieter

Hi Pieter,

I’ve been trying for a while now, I’ve messed around in the .h by putting the enum inside the class. Now it requires me to change the .cpp with the following error message:

C:\Users\c\Documents\Arduino\libraries\RTClib\RTClib.cpp:284:1: error: 'Ds1307SqwPinMode' does not name a type

Ds1307SqwPinMode RTC_DS1307::readSqwPinMode() {

^

exit status 1
Error compiling for board Arduino/Genuino Uno.

This applies to the following piece of code inside the .cpp:

Ds1307SqwPinMode RTC_DS1307::readSqwPinMode() {
 int mode;

 Wire.beginTransmission(DS1307_ADDRESS);
 Wire._I2C_WRITE(DS1307_CONTROL);
 Wire.endTransmission();
 
 Wire.requestFrom((uint8_t)DS1307_ADDRESS, (uint8_t)1);
 mode = Wire._I2C_READ();

 mode &= 0x93;
 return static_cast<Ds1307SqwPinMode>(mode);
}

It seemed obvious to me that since the Ds1307SqwPinMode is now inside the scope of the class RTC_DS1307, that I had to change the line:

Ds1307SqwPinMode RTC_DS1307::readSqwPinMode() {

to:

RTC_DS1307 Ds1307SqwPinMode ::readSqwPinMode() {

But that didn’t work… This is the output:

C:\Users\c\Documents\Arduino\libraries\RTClib\RTClib.cpp:284:12: error: 'Ds1307SqwPinMode' has not been declared

RTC_DS1307 Ds1307SqwPinMode ::readSqwPinMode() {

           ^

C:\Users\c\Documents\Arduino\libraries\RTClib\RTClib.cpp: In function 'RTC_DS1307 readSqwPinMode()':

C:\Users\c\Documents\Arduino\libraries\RTClib\RTClib.cpp:295:22: error: 'Ds1307SqwPinMode' does not name a type

  return static_cast<Ds1307SqwPinMode>(mode);

                     ^

exit status 1
Error compiling for board Arduino/Genuino Uno.

Could you give another hint?

Thank you for your help so far!

C.C.

Just add the word "class" on line change line 79: https://github.com/adafruit/RTClib/blob/8ff0034c319b1b4126afcaba2c0b25c0f2f6f439/RTClib.h#L79

enum class Ds1307SqwPinMode { OFF = 0x00, ON = 0x80, SquareWave1HZ = 0x10, SquareWave4kHz = 0x11, SquareWave8kHz = 0x12, SquareWave32kHz = 0x13 };