multiple definition of `rtc': <UsbFat.h> and "RTClib.h"

Hi guys,

I found this problem on several topics but still can’t figure out what’s wrong…
I have a project with 7 tabs. I use this hardware configuration:

  • Arduino Mega ADK
  • Adafruit PN532 (RFID card reader shield)
  • Adafruit ILI9341 (Touch screen shield)
  • 4GB Usb flash drive (FAT32 usb pluged in the usb host of the ADK)
  • Real-Time Clock Module DS1307

So my project worked perfectly fine before I had the librairy RTClib.h for the Real-Time Clock Module DS1307. Moreover the example codes for the time module work also without error.

Now I get the following error:

project.cpp.o:(.bss.rtc+0x0): multiple definition of `rtc'
CoffeeUsbFat.cpp.o:arduino-1.5.8\libraries\UsbFat/utility/ArduinoFiles.h:149: first defined here
/arduino-1.5.8/hardware/tools/avr/bin/../lib/gcc/avr/4.8.1/../../../../avr/bin/ld: Disabling relaxation: it will not work with multiple definitions
collect2: error: ld returned 1 exit status
Fehler beim Übersetzen

Here is how rtc is defined, I tried to change the name but it does nothing:

RTC_DS1307 rtc;

Here is what is in the ArduinoFiles.h file:

146:  /** \return number of bytes available from the current position to EOF
147:   *   or INT_MAX if more than INT_MAX bytes are available.
148:   */
149:  int available() {
150:    uint32_t n = FatFile::available();
151:    return n > INT_MAX ? INT_MAX : n;
152:  }

Do you have any idea how I could handle it ?

I tried with the DS1307RTC.h, but there is still a conflict with UsbFat.h.
I really have no idea of what I should do.

Any advise would be good !!

Ok typical/stupid error. I forgot to declare my rtc variable as static:

RTC_DS1307 rtc;.

Because when you use several files, you have to declare your librairies both in your .h files, and the main .ino files. Then, if you use a variable given by one of those librairies, you have to declare them as static to avoid multiple definition.

My bad

I forgot to declare my rtc variable as static:

In some cases objects need to be declared "extern". You will then have only one instance of the object.

Examples are the SPI object, the Wire object, and the SD object.

For SPI, the single object is defined in SPI.cpp:

SPIClass SPI;

Code in other files accesses the object by the "extern" declaration in SPI.h:

extern SPIClass SPI;