Undefined Reference Error on Compile

Racking my brain for the past few days over what I believe is a linker issue. I have a simple .h and .cpp file defined but when I try to compile the .ino program I receive an undefined reference error. I am using VS Code with CLI 1.7.1. Targeting a Boron on 1.4.0. Any guidance would greatly be appreciated!

Exact error:

undefined reference to `RTC_DS3231::begin()'
collect2: error: ld returned 1 exit status

Here is the code below:

rtc.ino

SerialLogHandler logHandler(ALL_LEVEL);
SYSTEM_MODE(SEMI_AUTOMATIC); 

#include "../lib/Adafruit-RTClib/RTClib.h"
RTC_DS3231 rtc;

void setup() {
  Serial.begin(115200); //open serial console
  delay(10000); //10 seconds

 rtc.begin();
}

void loop() {
  //Do nothing right now
}

RTClib.h

#ifndef RTCLIB_H
#define RTCLIB_H

class RTC_DS3231 {
public:
    bool begin();
};

#endif // RTCLIB_H

RTCLib.cpp

#include <Wire.h>
#include "RTClib.h"

boolean RTC_DS3231::begin() {
  Wire.begin();
  return true;
}

Huh? 1.47.0 is the most recent CLI version.
Can you check again via particle --version?

When your project structure is correct your include should not require the path

#include "RTClib.h"

should suffice.
Although commonly the main header file of a library should have the same name as the library. If you can't or don't want to change that, add a header file to the library like this

Adafruit-RTClib.h

#ifndef _ADAFRUIT_RTCLIB_H_
#define _ADAFRUIT_RTCLIB_H_
#include "RTClib.h"
#endif

While it shouldn't make a difference it's at least poor style to use bool in the header but boolean in the implementation.

Hey ScruffR,
Thanks for the response. Yes, I gave you the wrong version. I am using 1.47.0. I renamed the library to RTClib which matches the header file. I changed the bool to boolean so they both match. Still the same undefined reference error. If I don’t specify the path, the library is not found. The RTClib is in a lib folder at the root of the project. Where is the correct place? Here is a screenshot of my project directory.

The structure of the library is not correct IMO and it is missing a library.properties file.
This would be how a valid project structure should look like

>.
β”‚   project.properties
β”‚   README.md
β”‚
β”œβ”€β”€β”€.vscode
β”‚       launch.json
β”‚       settings.json
β”‚
β”œβ”€β”€β”€lib
β”‚   └───yourLib
β”‚       β”‚   library.properties
β”‚       β”‚   LICENSE.txt
β”‚       β”‚   README.md
β”‚       β”‚
β”‚       └───src
β”‚           yourLib.h
β”‚           yourLib.cpp
β”‚
β”œβ”€β”€β”€src
        yourProject.cpp
        yourProject.ino
2 Likes

Getting the correct folder structure for the library fixed the issue. It compiles now. Thanks again for the help!

1 Like