New to C++, error when running make command

Hi,
I am new to C++ but I like to structure my application is such away that it will be as clean as possible. To start learning I decide to use the veeeery simple blinker program, although it should be very easy I am not able to get it running. I made a header file (blinker.h), a class file (blinker.cpp) and the application.cpp file, see the files below.

When I run the make command I get the following error:

> obj/src/application.o: In function `loop':
> /Users/henkgescher/Spark/Temporary/core-firmware/build/../src/application.cpp:12: undefined reference to `Blinker::on(int)'
> /Users/henkgescher/Spark/Temporary/core-firmware/build/../src/application.cpp:13: undefined reference to `Blinker::off(int)'
> obj/src/application.o: In function `__static_initialization_and_destruction_0':
> /Users/henkgescher/Spark/Temporary/core-firmware/build/../src/application.cpp:5: undefined reference to `Blinker::Blinker(int)'
> /Users/henkgescher/Spark/Temporary/core-firmware/build/../src/application.cpp:5: undefined reference to `Blinker::~Blinker()'
> collect2: error: ld returned 1 exit status
> make: *** [core-firmware.elf] Error 1

What is the mistake I make?
Thanks,
Henk.

application.cpp
/* Includes ------------------------------------------------------------------*/
#include “application.h”
#include “blinker.h”

Blinker myLed(D7);

void setup() {
}

void loop() {
    myLed.on(5000);
    myLed.off(1000);
}

blinker.h

/* Includes ------------------------------------------------------------------*/
#include "application.h"

#ifndef BLINKER_H
#define BLINKER_H

class Blinker {
public:
    //Default Constructor
    Blinker();
    
    //Overload Constructor
    Blinker(int);
    
    //Destucture
    ~Blinker();
    
    //Accessor functions
    int getLed() const;
    
    //Mutator functions
    void setLed(int);
        //@param int - define the led
    
    //Function
    void on(int);
        //@param int - on time in msec
    
    void off(int);
        //@param int - off time in msec
    
private:
    // Member variables
    int definedLed;
    
protected:
};

#endif

blinker.cpp

/* Includes ------------------------------------------------------------------*/
#include "blinker.h"

Blinker::Blinker() {
}

Blinker::Blinker(int led) {
    definedLed = led;
    pinMode(definedLed, OUTPUT);
}

Blinker::~Blinker() {
}

int Blinker::getLed() {
    return definedLed;
}

void Blinker::on(int time) {
    digitalWrite(definedLed, HIGH);
    delay(time);
}

void Blinker::off(int time) {
    digitalWrite(definedLed, LOW);
    delay(time);
}

Hi @nika8991,

I haven’t tried compiling your code myself yet, but I’m guessing if you’re adding source files and building locally, that you should also make sure to add any new files to the “build.mk” file in the relevant directory, add something like the following, so the compiler knows where to find your code.

CPPSRC += $(TARGET_SRC_PATH)/blinker.cpp

Please let me know if this helps or not!

Thanks,
David

1 Like

Hi @Dave ,

Yes, that was the trick.
I am now able to make the program, to download it to the SparkCore and run it :-).

Thanks again,
Henk

1 Like