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 Automatically inserted prototypes lead to compiler errors (Read 1468 times)
Red Baron
Member
***
Offline


Posts: 112
Location: Germany
Joined: Jul 29th, 2015
Automatically inserted prototypes lead to compiler errors
Sep 2nd, 2018 at 9:00pm
Print Post  
Hi,

this simple program does not compile:
Code (C++)
Select All
class Foo{};
void bar(Foo f) {}

void setup() {}
void loop() {} 



The weird error message is: 2:10: error: 'Foo' was not declared in this scope.

The reason is the translation from .ino to .cpp into the build folder before compilation. Here the corresponding .cpp file:
Code (C++)
Select All
#include <arduino.h>
#line 1 "C:\\Users\\Ulli\\Documents\\Arduino\\CompileTest\\CompileTest.ino"
#line 1 "C:\\Users\\Ulli\\Documents\\Arduino\\CompileTest\\CompileTest.ino"

void bar(Foo f);
//
//

#line 1 "C:\\Users\\Ulli\\Documents\\Arduino\\CompileTest\\CompileTest.ino"
class Foo{};
void bar(Foo f) {}

void setup() {}
void loop() {} 



A prototype for 'bar' ist inserted before the class definition. The problem is the misleading error message. In a bigger project it took me several hours to find out the real problem.

Is it really necessary to generate these prototypes?

« Last Edit: Sep 2nd, 2018 at 9:01pm by Red Baron »  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12163
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Automatically inserted prototypes lead to compiler errors
Reply #1 - Sep 3rd, 2018 at 11:08am
Print Post  
Yes that is currently correct. 

You can add the prototype yourself with exact same signature at the correct location in your .ino core then visual micro will not attempt to insert the prototype at the top of the code before the user type has been created.

Code
Select All
class Foo{};
void bar(Foo f);

void bar(Foo f) {} 




Or you can use .cpp/.h

Or you can switch off auto prototype generation in project properties and create prototypes yourself same as you would do in .h/.cpp
  
Back to top
IP Logged
 
Red Baron
Member
***
Offline


Posts: 112
Location: Germany
Joined: Jul 29th, 2015
Re: Automatically inserted prototypes lead to compiler errors
Reply #2 - Sep 3rd, 2018 at 11:49am
Print Post  
Thanks for the hint. I did not consider the special behavior of an .ino file.

It is okay to put the prototype anywhere in the .ino file.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12163
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Automatically inserted prototypes lead to compiler errors
Reply #3 - Sep 3rd, 2018 at 12:04pm
Print Post  
It's okay but keep a few gotchas in mind

The .ino files are combined into a cpp in this order
   project_name.ino
   uppercase ino files
   lowercase ino files

We use this order because that is the order the arduino ide uses. It's not very scientific Smiley

So you just need to make sure your types are defined before the prototypes and prototypes before methods

Maybe the best place for a prototype is immediately before the first method that uses it but you might have a better idea.
« Last Edit: Sep 3rd, 2018 at 12:04pm by Tim@Visual Micro »  
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint