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 How does it make Visual Studio compile for Arduino? (Read 5922 times)
powervault
Newbies
*
Offline


Posts: 4
Joined: Jul 8th, 2016
How does it make Visual Studio compile for Arduino?
Jul 8th, 2016 at 2:46pm
Print Post  
Hi there

Does anyone know, generally speaking, how the Visual Micro extension works to compile and upload a sketch to the Arduino? I've heard that Visual Studio can only use the VS compiler, so what mechanism does it access the Arduino one instead and compile the hex file? What options in the IDE make it do this?

The reason I'm asking is because I'd like a single solution to be able to compile and upload for Arduino, and then in a different build configuration, build something different like a native Windows program. This is because I want to use Visual Studio's unit test suite for my Arduino projects, within the same Solution as the project itself. I imagine this would involve creating a new build configuration (called e.g. "unit test"), excluding the main .ino file from the build, using preprocessor directives to compile some bits of the code differently (e.g. not #include'ing Arduino.h, I'd imagine, and exporting the methods and properties under test in the same way as you would with a .dll), and changing the project properties. And some other stuff too I'm guessing. I managed to hack an Arduino project into something that's unit test-able by doing the above, so I guess it's possible, but I would definitely like to do it properly by understanding what I'm doing. If anyone has any info, in particular on what Visual Micro makes Visual Studio do to compile for Arduino, and what options/buttons/project properties/etc inside the environment make it do that?

Thanks!

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


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: How does it make Visual Studio compile for Arduino?
Reply #1 - Jul 8th, 2016 at 3:29pm
Print Post  
Hi,

It's a good question. I can give some suggestions but I would be keen to find a smooth solution that can be documented.

1)
There are two ancient existing "tools>option>visual micro>debug configuration" and "release configuration" where you can specify the config names that will be used for Arduino builds, but I just tested them and they are no longer working. Will try to fix for the next release. By entering configuration names in the options Visual Micro will leave the VS compiler to attempt it's own compilation.

2)
You could create a 2nd project in the same solution and link (add existing file>as link) all the files you want to use to build a normal VC++ project.

3)
For paid users, in the next release it is possible to "import the arduino core code" locally. You can already do this with libraries but the next release makes it easier with an 'Import Libraries' function. By having the core and libraries locally it might help you but I guess you can work out where they are and set the correct include paths.

..

I guess option 1) would be most useful but can you think of other options that will help. Ideally we would have specific configurations for Arduino projects but that would be confusing for new users who have difficulty switching between Release and Debug







  
Back to top
IP Logged
 
powervault
Newbies
*
Offline


Posts: 4
Joined: Jul 8th, 2016
Re: How does it make Visual Studio compile for Arduino?
Reply #2 - Jul 8th, 2016 at 4:07pm
Print Post  
Great, thanks for this - it's useful to know about point 1) in particular. I've actually recently noticed that Ctrl->F5 and F5 aren't automatically making the Micro compiler compile, it tries to use the c++ compiler instead (error list includes errors like "macro name '__cplusplus' is reserved, '#define' ignored" and so on). Could this be the reason? (I'm on VM version 1606.17.10). I've also noticed that the "assembly location" option gives the location of a .dll - is this what makes it assemble, i.e. the compiler is the same for an Arduino build compared with a Windows Native one, but the assembler different?

I'll have a play with option 2. As long as it lets me switch between the two using build configurations, in the same solution, I don't mind. If option 1) isn't working then this would be a good workaround, although it would be more direct to do it that way instead. (Are any legacy versions available?) Nice to know about 3) as well.

This has given me something to go on and I'm determined to get this set up! I'm making a how-to as part of this for my own use so I'd be happy to provide this to you when I'm done if you're happy to provide support? I'd appreciate that!

Many thanks

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


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: How does it make Visual Studio compile for Arduino?
Reply #3 - Jul 8th, 2016 at 4:18pm
Print Post  
Yes the Arduino build is very different to the VS build so Visual Micro overrides and handles it.

There are some settings to control if Visual Micro will build the startup project but the default is probably what works best for you... If Arduino code is active in the editor, then Visual Micro will build (the project being edited) otherwise it lets VS do what it normally does which is to build the startup project. In your case the VC++ project can be the startup project so F5. Let me know if you hit problems with that and I will explain some more.


errors like "macro name '__cplusplus' is reserved

There is a file called __vm\[projectName].vsarduino.h which Visual Micro does NOT compile. The file is only used to boost the intellisense for Arduino projects. You should not attempt to compile the file because it will not be valid. You could try to change it's property to "content" as opposed to "compile" but again if you find it getting in the way then let me know and we can look at some new options to help.

...

There is a new release due over the next few days and I will try to re-invoke 1). Arduino has changed so much in the last 6 months there is little point in stepping back to an older visual micro version.
  
Back to top
IP Logged
 
powervault
Newbies
*
Offline


Posts: 4
Joined: Jul 8th, 2016
Re: How does it make Visual Studio compile for Arduino?
Reply #4 - Jul 8th, 2016 at 4:45pm
Print Post  
Ah ok. With the "code being active in the editor, then F5 to build" feature, this isn't working. I think I broke it when I was playing with all the properties for everything. Do you know how I might fix it? Also, do you mind pointing me towards the settings you mentioned anyway, in case they come in useful?

I guess that the compiler errors I was seeing was because the visual studio compiler was trying to compile the above file. Which was because of the above "F5" error.

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


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: How does it make Visual Studio compile for Arduino?
Reply #5 - Jul 8th, 2016 at 7:58pm
Print Post  
It's "tools>options>visual micro>application features>upload/debug active code". The default is True.

With it set to False, an arduino project will need to be the startup project for F5 to work.
« Last Edit: Jul 8th, 2016 at 8:00pm by Tim@Visual Micro »  
Back to top
IP Logged
 
powervault
Newbies
*
Offline


Posts: 4
Joined: Jul 8th, 2016
Re: How does it make Visual Studio compile for Arduino?
Reply #6 - Jul 14th, 2016 at 4:22pm
Print Post  
Hi Tim

Thanks for that. The F5 button press doesn't work either when this is true or when the arduino code is the startup project. I'm not sure what I've done.

I did however get unit testing working. I did it by excluding certain bits (including the .ino file) from the build, building it as a .dll, and exporting the methods I wanted to test. Here are the instructions. I would point out to anyone wanting to do this that the work doesn't stop there: if you're testing any code that uses Arduino core functions, you'll need to mock these functions up when testing it. It'd be prudent to take this into account as well when judging whether unit testing will be an overall investment in time.

Anyway, here it is, I've just dumped my own notes in here so they're a bit rough but hopefully still useful.

To add unit testing to an existing Arduino project:

1) Create a build configuration called "UnitTest". Copy settings from "release" and create new project configurations.
(I did this for a Win32 configuration. Does it work just as well for x64?)

2) For the project properties of the original arduino project:
- exclude .ino and [projname].arduino.h from build in the project properties (and change "content" to 0 in the same place?)
- in the project properties:
  - change configuration type to a .dll
  - C/C++ Preprocessor definitions: (delete all arduino-related ones if they exist), add "UNIT_TEST" and "_VSARDUINO_H_"
  - C/C++ Additional include directories: (delete references to all arduino-related folder locations if they exist)
  - note that the above two are overwritten when compiling for Arduino, so you'll have to go back and do this every time you switch
    back to unit test after compiling for Arduino. In fact the include directories are overwritten when you switch over.

3) Create a native unit test C++ project in the same solution and:
- add a reference to the other project
- #include the header files in the unit test c++ file for the classes containing the methods you want to test. May need to
  specify e.g. "../[filename]", or the full path, or maybe fiddle around with the include directories in the properties
- add the same preprocessor definitions as the arduino project. (could you do this in the solution properties then?)

3) In the classes you want to test:
- add "_declspec(dllexport)" between "class" and the classname in the header file where the class is being declared.
- set up a macro (called "[CLASSNAME]_API" or similar to compile the above storage class specifier for the UnitTest
configuration but not for the main Arduino build


4) As you write the tests/code:
- Need to keep going into Arduino.h and #including the files and writing the #defines and typedefs that are used in your code
and putting this into a section #if defined(UNIT_TEST) in the header files.


Notes
- make sure they're all set to the same platform in the solution properties -> configuration
- this turns the arduino project into a .dll. is this the best way to do it? Suppose it's as good as any.

Enjoy!

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


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: How does it make Visual Studio compile for Arduino?
Reply #7 - Jul 14th, 2016 at 6:20pm
Print Post  
Thanks for the update
  
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint