Undefined reference to `function::function(variable)' collect2.exe: error: ld returned 1 exit status

Dear Forum,

i dont where im doing wrong. below code works fine in Arduino IDE but unsuccessful in PIO with arduino core

Here in what i ve written in

Main.cpp

#include <Arduino.h>
#include "nav_button.h"

#define BUTTON_PIN 10

Button button1(BUTTON_PIN);

void setup() {
  Serial.begin(9600); 
}

void loop() {
}

and in CPP file →

nav_button.cpp

#include “nav_button.h”
Button::Button(byte pin) {
this->pin = pin;
init();
}
void Button::init() {
pinMode(pin, INPUT);
}

and in .h file →

#include <Arduino.h>
#ifndef MY_BUTTON_H
#define MY_BUTTON_H
class Button {
private:
byte pin;
public:
Button(byte pin);
void init();
};
#endif

when i compile above, below error is throwing. how to come out of it. can someone please help?

Linking .pio\build\miniatmega328\firmware.elf
C:\Users\Venu’s\AppData\Local\Temp\ccPBFI4M.ltrans0.ltrans.o: In function global constructors keyed to 65535_0_main.cpp.o.1705': <artificial>:(.text.startup+0x5e): undefined reference to Button::Button(unsigned char)’
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\miniatmega328\firmware.elf] Error 1
=========================================================== [FAILED] Took 7.97 seconds ===========================================================The terminal process “C:\Users\Venu’s.platformio\penv\Scripts\platformio.exe ‘run’” terminated with exit code: 1.

Adn where is this file located? In src/ right?

Hi

Yes, correct. both .h and .cpp are in same “lib” folder. along with other .h files i made.

You should create a subfolder in lib and put the source files in there, as the README says, not directly into lib/.

1 Like

Thanks dear… due to time constraints i ve copied and pasted all CPP contents in .h file and i succeeded in compiling,
in future i will use your suggestion.

thank you again.