#undefined reference

12 messages · Page 1 of 1 (latest)

rich swan
#

I'm playing around with an ESP32 and C++

#include "OnBoardLED.cpp"
#include "esp_log.h"
#include <chrono>
#include <thread>

class Main {
    const char *TAG = "Main";

public:
    Main() { ESP_LOGI(TAG, "Constructor"); }

    ~Main() { ESP_LOGI(TAG, "Destructor"); }

    void run() {
        OnBoardLED led;
        while (true) {
            led.on();
            std::this_thread::sleep_for(std::chrono::seconds(1));
            led.off();
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    }
};

extern "C" {
void app_main(void) {
    Main main;
    main.run();
}
}

I don't fully understand why I get a compile error: undefined reference to OnBoardLED::OnBoardLED()collect2: error: ld returned 1 exit status.

fallen egretBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

rich swan
#

Does it happen because I don't use a header file for OnBoardLED?

solemn gyro
#

OnBoardLED::OnBoardLED() - where is that constructor defined?

rich swan
#

OnBoardLED.cpp

#include "esp_log.h"

class OnBoardLED {
private:
    const char *TAG = "OnBoardLED";

public:
    OnBoardLED();
    void on() { ESP_LOGI(TAG, "Switching LED on"); }
    void off() { ESP_LOGI(TAG, "Switching LED off"); }
};
solemn gyro
#

But where's the body for OnBoardLED();?

#

You declared it but never defined

rich swan
#

Ah. I see.

solemn gyro
#

But yes, you shouldn't include .cpp files

#

This one looks like it should be renamed to .h, then it'll just work as a header (add #pragma once to the beginning too)

rich swan
#

Thank you.

#

!solved