Hi guys,
Im starting to develop in C++ and I developed this class
the current structure is
main.ino
buzzer/
** Buzzer.cpp**
** Buzzer.h**
buzzer/Buzzer.h
#ifndef BUZZER_H
#define BUZZER_H
#include <Arduino.h>
class Buzzer {
public:
Buzzer(int pin);
void beep();
private:
int _pin;
};
#endif
buzzer/Buzzer.cpp
#include <Arduino.h>
#include "Buzzer.h"
Buzzer::Buzzer(int pin) {
_pin = pin;
pinMode (_pin, OUTPUT) ;
digitalWrite(_pin, HIGH);
}
void Buzzer::beep() {
digitalWrite(_pin, LOW);
delay(80);
digitalWrite(_pin, HIGH);
delay(80);
digitalWrite(_pin, LOW);
delay(80);
digitalWrite(_pin, HIGH);
delay(80);
digitalWrite(_pin, LOW);
delay(200);
digitalWrite(_pin, HIGH);
}
on my main.ino:
#include "buzzer/Buzzer.h"
Buzzer buzzer = Buzzer(10);
The problem is when compiling:
============== Begin to compile. ==============
/var/folders/3g/jmz5d_516l58p5dq9h83s4xc0000gn/T//ccr4otWa.ltrans0.ltrans.o: In function __static_initialization_and_destruction_0':* */Users/jjalonso/Work/fpa/src/main.ino:8: undefined reference to
Buzzer::Buzzer(int)'
collect2: error: ld returned 1 exit status
exit status 1
Any help?
thanks
Don't put your class in a sub-folder. I don't think that will work. You could make it into a library.
#include "buzzer/Buzzer.h"
Or try:
#include "/buzzer/Buzzer.h"
To get the right path to your files...
I've confirmed that this is the case. The IDE does not pick up on CPP files that are in a subdirectory in the sketch folder. It compiles with a flat structure, and fails with the files in a subfolder.
Not surprised. The IDE copies your files into an intermediate folder. It clearly doesn't recursively copy sub-folders. Thus as far as it is concerned the Buzzer.cpp file doesn't exist. Hence you get a linker error at link time.
Actually it does, but whatever script is used to check for source files to compile doesn't look in subdirectories, so the .cpp file isn't compiled and linked. Header files will still work fine this way, that's why the error is about an undefined reference to the Buzzer constructor, and not that it couldn't find the header file.
Then, I should put all files in root? or at least the cpp next to the main ?
I didnt changed it still but Im looking the build folder of my IDE and is creating this.
Buzzer.cpp and Buzzer.h are generated inside the buzzer folder succesfully. Then really I cannot understand why cannot find it.
About routes, the routes of include are right because if I add any different to the URL it drop a not found file error.
Okey, I will create a begin for any instruction.
About the import now i put everything in root and its working... Quite sad I cannot create folders with modules to organise my code.