Undefined Reference Error on Compile

Tags: #<Tag:0x00007fe21ef791c8>


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:


SerialLogHandler logHandler(ALL_LEVEL);

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

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


void loop() {
  //Do nothing right now


#ifndef RTCLIB_H
#define RTCLIB_H

class RTC_DS3231 {
    bool begin();

#endif // RTCLIB_H


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

boolean RTC_DS3231::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


#include "RTClib.h"

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
│       launch.json
│       settings.json
│   └───yourLib
│       │   library.properties
│       │   LICENSE.txt
│       │   README.md
│       │
│       └───src
│           yourLib.h
│           yourLib.cpp


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