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] 2  Send TopicPrint
Hot Topic (More than 8 Replies) Sharing variables between modules (Read 21640 times)
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Sharing variables between modules
Mar 27th, 2014 at 12:23pm
Print Post  
Hi,

I agree this is not regarding VisualMicro, but I have an annoying, hum annoyance  Roll Eyes.

Because the program is quite long, I decided to split the code in two modules: the .ino plus a second one, a .cpp file. The annoyance I have is that the .cpp module can't access variables and functions in the .ino module (not defined in this context). For now in the .cpp module I declare things like this:

Code
Select All
extern float foo 


But that means a lot of extern statements.

Is there an easier way to share the variables and functions among the project ?

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


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Sharing variables between modules
Reply #1 - Mar 27th, 2014 at 1:01pm
Print Post  
Hi Gilles,

I think using multiple .ino files instead of cpp is a better solution for you.
  
Back to top
IP Logged
 
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Re: Sharing variables between modules
Reply #2 - Mar 27th, 2014 at 1:04pm
Print Post  
Shocked I didn't know that would work, I will try and let you know.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Sharing variables between modules
Reply #3 - Mar 27th, 2014 at 1:08pm
Print Post  
It certainly will work and is a big part of Arduino. The thing to know is that only the sketchname.ino needs the library or other #includes.

All .ino files share everything and are automatically combined into a single cpp by the compiler.

So all functions and variables are shared between the .ino files. No need to do anything different to make it work. No need for extern etc.
  
Back to top
IP Logged
 
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Re: Sharing variables between modules
Reply #4 - Mar 27th, 2014 at 1:37pm
Print Post  
Tim,

you make my day  Smiley.

But one thing though: intellisense does not work as it does in the main .ino file. WHat I mean is that in my second .ino file, when I start to type a variable name, intellisense does not display suggestions as it does in the main .ino file. It looks like it will only suggest variables when the name is already used in the file.

Is it a parameter I need to change ?

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


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Sharing variables between modules
Reply #5 - Mar 27th, 2014 at 1:44pm
Print Post  
I need to look into that it's not right. Which Ide are you using the intellisense works differently in them all.
  
Back to top
IP Logged
 
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Re: Sharing variables between modules
Reply #6 - Mar 27th, 2014 at 1:49pm
Print Post  
Atmel Studio 6 (Version: 6.1.2730 - Service Pack 2)

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


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Sharing variables between modules
Reply #7 - Mar 27th, 2014 at 3:27pm
Print Post  
Ok great that is clear for me thanks. I know functions work but forgot to check if the variables are coming through. 

I hope this can be forced in the next version of vm, it depends on how VisualAssistX works
  
Back to top
IP Logged
 
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Re: Sharing variables between modules
Reply #8 - Mar 27th, 2014 at 3:33pm
Print Post  
Quote:
I hope this can be forced in the next version of vm.

Has a new release been officially released ? I saw 1403.26 in another post, but nothing about that version in the Release Notes & Notifications forum ?

I can live for intellisense forgetting about the variables, not having to use extern declarations is already a big +  Grin.

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


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Sharing variables between modules
Reply #9 - Mar 27th, 2014 at 3:37pm
Print Post  
Great. I usually issue beta versions in the beta channel just prior to an official release. This allows a more controlled test by approx 50 users per day. If I make a new official release everyone gets notified and then hundreds download it. Any bugs and my life ends  Smiley

I don't issue early beta's so it's usually ok to take. This one has been quite well tested now but I don't think it contains anything useful for you.

http://visualmicro.codeplex.com/releases/view/106554

It's mainly fixes for Atmel 6.2, better trace diagnostics, improved library support and workarounds for users with a corrupted windows wmi database. 

However the more people that test it the better  Smiley
  
Back to top
IP Logged
 
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Re: Sharing variables between modules
Reply #10 - Mar 27th, 2014 at 6:42pm
Print Post  
Tim,

I think I found out another issue working with two .ino files. When there is a compile error in the second file, the message states the error is in the main file on a non existing line. I guess this is because for compilation purposes, all is combined in a single file  Shocked.

Looks like I will have to go back to a .cpp file  Cry.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Sharing variables between modules
Reply #11 - Mar 27th, 2014 at 8:35pm
Print Post  
Hi Gilles,

Quote:
I think I found out another issue working with two .ino files. When there is a compile error in the second file, the message states the error is in the main file on a non existing line. I guess this is because for compilation purposes, all is combined in a single file 


You don't need to use cpp files there is a lot of support in Visual Micro to handle the complexities of a combined cpp. Yes there can be more but I think we can work through this with a little discussion.

It is wrong that the temp copy in the build folder was displayed, Visual Micro is supposed to handle that but a few variations sometimes slip through the net. I would like to see your code or similar and understand the error and where it was. 

Here is a simple test

I create a source called blink2.ino in my blink project. I added the following code with an error in the definition of a2. The compiler correctly gave me an error for the line and when I double clicked I was taken to it.

Code
Select All
int a1;
int a 2;

void test()
{
}

void blah()
{
}
 



Some points to note.

Sometimes there is more than one line to an error. Clicking the lowest line is usually the one that you need.

By default, when you hit F5 for debug upload two compilations happen. 1) Your normal code, 2) your code with debug breakpoints. Sometimes the breakpoints can produce errors and Visual Micro does need to handle these better in terms of tying the errors back to the source
« Last Edit: Mar 27th, 2014 at 9:00pm by Tim@Visual Micro »  
Back to top
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Sharing variables between modules
Reply #12 - Mar 27th, 2014 at 9:09pm
Print Post  
2)

Quote:
But one thing though: intellisense does not work as it does in the main .ino file. WHat I mean is that in my second .ino file, when I start to type a variable name, intellisense does not display suggestions as it does in the main .ino file. It looks like it will only suggest variables when the name is already used in the file.


I've just tested this and it's working okay in Atmel Studio, not as well as VS but it's not bad.

I agree that for some reason the VisualAssistX intellisense engine does not list the variables from other code files. 

However when you add the variable to the code it is accepted, no red squiggles and right mouse click "Go To Definition" correctly takes you to the definition in the other .ino. 

So this shows that VisualAssistX knows about the code but doesn't show it in the intellisense prompt. 

I'll see if I can find someway to force it to work better and get back to you
  
Back to top
IP Logged
 
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Re: Sharing variables between modules
Reply #13 - Mar 27th, 2014 at 9:20pm
Print Post  
My bad  Embarrassed.

Here is the contents of MicroBuild:

Code
Select All
Compiling 'UF_TSNOS_1' for 'Mega Pro 2560V 3.3V'
Build folder:
UF_TSNOS_1.ino:In function 'void ML12100()'
Menus.ino:2850: error: 'pResol' was not declared in this scope
Error compiling
 


If I click on the 3rd line - UF_TSNOS_1.ino... - I am brought to line 1 of this file which is the project .ino file.

But if I click on 4th line, - Menus.ino:2850... I am brought to the correct line in Menus.ino. 2850 is not the correct line number, but since the correct line is highlighted, that fine.

No one should code Arduino without AtmelStudio+VisualMicro  Wink.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Sharing variables between modules
Reply #14 - Mar 27th, 2014 at 9:23pm
Print Post  
Okay thanks very much. The textual error messages have recently been improved and I think the display of the line number needs some re-work. 

What this means is that Visual Micro know the correct line which is good news for me Smiley

Great report thanks there is a fix in the beta downloads area
« Last Edit: Mar 28th, 2014 at 2:51am by Tim@Visual Micro »  
Back to top
IP Logged
 
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Re: Sharing variables between modules
Reply #15 - Mar 28th, 2014 at 12:26pm
Print Post  
Hi Tim,

this morning again clicking on a compile error message does not bring me to the right code line  Shocked. I think it may not have to do with VisualMicro code.

Are you aware of any situation that could 'fool' VisualMicro ? I came across situations a few times to that would fool intellisense in VB for example. A line of code would break the editor and it behaved strangely.

My code compile and upload fine - when there is no obvious bug -, but it acts strangely. I get int variables corrupt when I manipulate strings and send them to an LCD by means of a serial port. The strings are global and the int are local, really weird. I know this look like out of this world, but I suspect a condition that  makes it way through the compiler but is wrong and induces issues.

Or maybe I drink to much water  Embarrassed.

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


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Sharing variables between modules
Reply #16 - Mar 28th, 2014 at 12:30pm
Print Post  
I think you probably need to read some more about strings but the questions are standard code questions unrelated to the debugger or visual micro so you can use the arduino.cc forum

If you do not zero terminate your strings you can overwrite memory. If your code is correct then this will not happen.

If a dbl click takes you to the wrong lease then please open a new thread and report what the error was

I also think that any continued discussion of strings/memory should now in a new thread.

Thanks
  
Back to top
IP Logged
 
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Re: Sharing variables between modules
Reply #17 - Mar 28th, 2014 at 12:39pm
Print Post  
Tim,

I know well C and the ways strings are handled, and how you can easily poison your life  Sad.

I started coding in C at the age of CP/M: 64K memory, and forget about IDE and breakpoint... stone age  Cheesy.

I also came across a bug on a Rabbit  MCU where the compiler incorrectly handled strings, to solve the issue I had to reorder variables declarations. The time it took to find out that...
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Sharing variables between modules
Reply #18 - Mar 28th, 2014 at 12:42pm
Print Post  
Oh good, then interested to hear what you discover about Arduino memory so we can post it on the wiki thanks  Smiley
  
Back to top
IP Logged
 
Gilles Plante
Full Member
***
Offline


Posts: 156
Location: Québec, Canada
Joined: Oct 31st, 2013
Re: Sharing variables between modules
Reply #19 - Mar 28th, 2014 at 1:15pm
Print Post  
Well,

the bug has been located standing about 16" inches from the monitor Grin.

It was a darn byte vector that was sized too small Embarrassed. It could have been a char string, I would have had the same issue. Every string is huge pitfall in C.

Arduino has a new String object that automatically grow itself, much like in old VB. I read that there are some glitches though. I have used it in another project, and found out that one method of the class behaves badly, that is length(): MyString.length() is equivalent to strlen(MyCharArray). Other than that , I was happy with it. It's very easy to use, with loads of methods like .substring() and .indexOf() for example. Reference is here http://arduino.cc/en/Reference/StringObject Higly recommended.

Sure, it uses more memory than conventional char arrays, but if your MCU has plenty of RAM and FLASH, it has many advantages.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send TopicPrint