[Solved] Undefined reference to own class used in Arduino sketch .cpp
-
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> }
-
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