I have been trying to learn how to break up my now rather large project into smaller header(.h) and source (.cpp) files.
I just spent the last 2 hours trying to work out a weird error when I just reopened the project into Arduino IDE only to find it works there but not VS Studio/VS Micro.
This is just test code which I have adapted for a blinky.
// led.ino
#include "Arduino.h"
#include "led.h"
void setup(){
led.Setup();
}
void loop()
{
led.Blink();
}
// led.h
#ifndef _LED_h
#define _LED_h
class ledclass
{
public:
ledclass();
void Setup();
void Blink();
};
extern ledclass led;
#endif
// led.cpp
#include "Arduino.h"
#include "led.h"
const int pin = 13; //LED pin
ledclass::ledclass() {}
void ledclass::Setup(){
pinMode(pin, OUTPUT);
}
void ledclass::Blink() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
ledclass led = ledclass();
Compilation results in VS Studio/VS Micro Compiling 'led' for 'Arduino Uno'
ccuFuCY6.ltrans0.ltrans.o*: In function main
main.cpp:43: undefined reference to setup
main.cpp:46: undefined reference to loop
collect2.exe*: error: ld returned 1 exit status
ccy1vwSd.ltrans0.ltrans.o*: In function main
Error linking for board Arduino Uno
main.cpp:43: undefined reference to setup
Build failed for project 'led'
main.cpp:46: undefined reference to loop
collect2.exe*: error: ld returned 1 exit status
I switched to an Uno just for simplicity but I get the same results on my ATtiny3216, which is not really surprising as it's stuck at compile.
I tried starting with a blank project called led.ino, pasted the source in then used the VS micro > Add Code > Add empty .cpp and Header. I then pasted the source into the .h and .cpp files but I just get the same error under compilation.
If I open the led.ino under Adruino IDE it all works.
I am completely new to using .cpp and .h files so it could well be what I am doing but I would have expected it to run in both.
Thanks in advance for any help.