Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic [SOLVED] Compile errors in VS studio/VS micro but not in Arduino IDE (Read 1591 times)
castingflame
Junior Member
**
Offline


Posts: 20
Joined: Nov 22nd, 2018
[SOLVED] Compile errors in VS studio/VS micro but not in Arduino IDE
Feb 6th, 2021 at 2:56pm
Print Post  
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.

Code
Select All
// led.ino
#include "Arduino.h"
#include "led.h"

void setup(){
  led.Setup();
}

void loop()
{
  led.Blink();
} 




Code
Select All
// led.h
#ifndef _LED_h
#define _LED_h

class ledclass
{
	public:
		ledclass();
		void Setup();
		void Blink();
};
extern ledclass led;
#endif 




Code
Select All
// 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

Code
Select All
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.


 
« Last Edit: Feb 7th, 2021 at 9:21am by castingflame »  
Back to top
 
IP Logged
 
davidcoward
Junior Member
**
Offline


Posts: 48
Joined: Oct 5th, 2012
Re: Compile errors in VS studio/VS micro but not in Arduino IDE
Reply #1 - Feb 6th, 2021 at 3:58pm
Print Post  
The cpp contains code related to the specific instance, so you don't need/shouldn't do:

const int pin = 13;  //LED pin

ledclass::ledclass() {}

or

ledclass led = ledclass(); 

These won't work because you haven't created an instance of the class in your ino.

In your .h add something like:

private:
     uint8_t ledPin;

And change it to, say

public:
     void init(uint8_t pin);
     void Blink();

Then refer to them in cpp with (for example):

void led::init(uint8_t pin) {
     this->ledPin = pin;
     pinMode(pin, OUTPUT);
}

And :

digitalWrite(this->ledPin, HIGH);

In your .ino, you initiate a specific instance of the class like this:

ledclass myLED;

Then in setup:

myLED.init(LED_BUILTIN);

And in loop:

myLED.Blink();

Then you can create multiple instances of the class for different pins (for example).

The Arduino toolchain is designed to hide the fiddly bits of OOP (and does it well) so builds things differently to Visual Micro so that's why it works there and not here...



« Last Edit: Feb 6th, 2021 at 3:59pm by davidcoward »  
Back to top
 
IP Logged
 
castingflame
Junior Member
**
Offline


Posts: 20
Joined: Nov 22nd, 2018
Re: Compile errors in VS studio/VS micro but not in Arduino IDE
Reply #2 - Feb 6th, 2021 at 10:54pm
Print Post  
Brilliant, thanks David. Cool

I have seen so many ways to do the breaking up of the project into smaller chunks but a lot of them seem like hacks. I am trying to choose a more traditional C++ way of doing it. I must confess at being left a little confused now after trying various approaches and the one I posed was based on a YouTube example. I'll take in your changes in the morning and give it a go. I really appreciate your help and I'm sure it will make more sense when its all working and I can dissect it.  Smiley



  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Compile errors in VS studio/VS micro but not in Arduino IDE
Reply #3 - Feb 7th, 2021 at 2:38am
Print Post  
Thanks both, if your project and master .ino are called "led" then also make sure you do not create an led.cpp. That can cause confusion because the .ino code will be ignored.
  
Back to top
WWW  
IP Logged
 
castingflame
Junior Member
**
Offline


Posts: 20
Joined: Nov 22nd, 2018
Re: Compile errors in VS studio/VS micro but not in Arduino IDE
Reply #4 - Feb 7th, 2021 at 9:21am
Print Post  
Tim@Visual Micro wrote on Feb 7th, 2021 at 2:38am:
Thanks both, if your project and master .ino are called "led" then also make sure you do not create an led.cpp. That can cause confusion because the .ino code will be ignored.


I tried just renaming the led.ino to diode.ino and it all burst into life straight away. I think we'll call that one a gotcha!


I will also go over David's suggestions and try to do it the correct way.

Thanks guys  Wink
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint