Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. [Solved] Undefined reference to own class used in Arduino sketch .cpp

[Solved] Undefined reference to own class used in Arduino sketch .cpp

Scheduled Pinned Locked Moved Troubleshooting
2 Posts 1 Posters 4.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DerTomm
    wrote on last edited by DerTomm
    #1

    Hi,

    I just ran into a problem which occures as soon as I include another own class besides MySensors. Here is my code:

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    // Enable serial gateway
    #define MY_GATEWAY_SERIAL
    
    // Set blinking period
    #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Inverses the behavior of leds
    #define MY_WITH_LEDS_BLINKING_INVERSE
    
    // Enable inclusion mode
    #define MY_INCLUSION_MODE_FEATURE
    // Enable Inclusion mode button on gateway
    //#define MY_INCLUSION_BUTTON_FEATURE
    
    // Inverses behavior of inclusion button (if using external pullup)
    //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
    
    // Set inclusion mode duration (in seconds)
    #define MY_INCLUSION_MODE_DURATION 60
    // Digital pin used for inclusion mode button
    #define MY_INCLUSION_MODE_BUTTON_PIN  3
    
    #include <SPI.h>
    #include <MySensors.h>
    #include "Applications/LedHandler.h"
    
    MyMessage msgNodeTemperature(0, V_TEMP);
    
    LedHandler* ledHandler;
    
    unsigned long lastSentTimestamp = 0L;
    
    void setup() {
      ledHandler = new LedHandler();
    }
    
    void presentation() {
    // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Demo", "1.0");
      present(0, S_TEMP);  // Board ambient temperature
    }
    
    /****************************************************************************************************************
     *
     */
    void loop() {
    
      if (millis() - lastSentTimestamp > 10000) {
    
        long int rand = random(0, 30);
        send(msgNodeTemperature.set(rand, 1));
    
        lastSentTimestamp = millis();
      }
    }
    

    The problem starts when I create a new object of LedHandler and assign it to the pointer in the setup() method. Then I get the following linker error:

    Compiling .pioenvs\nanoatmega328\src\OpenhabDemo.o
    Linking .pioenvs\nanoatmega328\firmware.elf
    .pioenvs\nanoatmega328\src\OpenhabDemo.o: In function `setup':
    C:\Users\Thomas\.platformio\lib\MySensors_ID548/drivers/ATSHA204/sha256.cpp:70: undefined reference to `LedHandler::LedHandler()'
    collect2.exe: error: ld returned 1 exit status
    *** [.pioenvs\nanoatmega328\firmware.elf] Error 1
     [ERROR] Took 2.97 seconds
    

    Obviously there is a dependency problem. Can you help me how to get rid of it?
    I use PlatformIO for compilation. OpenhabDemo.cpp is my main Arduino sketch file.

    Here is the header and the class file of the dependend LedHandler:

    #ifndef SRC_APPLICATIONS_LEDHANDLER_H_
    #define SRC_APPLICATIONS_LEDHANDLER_H_
    
    #include "Actor.h"
    #include "ApplicationHelper.h"
    
    class LedHandler: public Actor {
    
    public:
    
      LedHandler();
      virtual ~LedHandler();
    
      uint8_t getApplicationPort() const {
        return ApplicationHelper::LED_HANDLER;
      }
    
      void executeFunction(char* payload);
    
      void addLed(uint8_t ledPin);
      bool setLedEnabled(uint8_t ledId, bool enabled);
    
    private:
    
      uint8_t* managedLeds;
      uint8_t ledCounter;
    
    };
    
    #endif /* SRC_APPLICATIONS_LEDHANDLER_H_ */
    
    #include "Applications/LedHandler.h"
    
    /***************************************************************************************************
     *
     */
    LedHandler::LedHandler() {
      ledCounter = 0;
      managedLeds = 0;
    }
    
    /***************************************************************************************************
     *
     */
    LedHandler::~LedHandler() {
      if (managedLeds != 0) {
        delete managedLeds;
      }
    }
    
    /***************************************************************************************************
     *
     */
    void LedHandler::executeFunction(char* payload) {
     <deleted>
    }
    
    /***************************************************************************************************
     *
     */
    void LedHandler::addLed(uint8_t ledPin) {
       <deleted>
    }
    
    /***************************************************************************************************
     *
     */
    bool LedHandler::setLedEnabled(uint8_t ledId, bool enabled) {
      <deleted>
    }
    
    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerTomm
      wrote on last edited by
      #2

      Finally I found a solution for the described situation.

      PlatformIO did not compile all dependend files in a library due to a wrong folder layout. After correcting this it works now.
      For people experiencing the same issue I recommend visiting the connected PlatformIO community thread: https://community.platformio.org/t/pio-does-not-compile-all-included-library-classes-which-leads-to-linker-errors/1034

      1 Reply Last reply
      1

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      14

      Online

      12.0k

      Users

      11.2k

      Topics

      113.4k

      Posts


      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • MySensors
      • OpenHardware.io
      • Categories
      • Recent
      • Tags
      • Popular