Introduction: Arduino Shared Projects
When developing your Arduino projects there is often code which is best shared so multiple projects can use it.
Visual Micro also allows use of Visual Studio C++ Shared Projects, allowing us to develop this code separately, and include it into multiple projects.
Step 1: How Does It Work?
The Shared Project is combined with the Arduino project before it is compiled.
The Arduino Project knows to include the Shared Project due to the Reference (added to Arduino Project in Step 4)
When combined all files from the Shared Project root folders are combined with the Arduino Projects root folder.
To avoid any file name conflicts, we highly recommend you place your Shared Project code within an src\ProjectName\ folder (read on to see why..)
Step 2: Create Shared Project
First create a Shared Project using the template in Visual Micro.
Highlight the Shared Project in the Solution Explorer, and click the "Show All Files" Icon at the top of the window.
This will let us see what is happening more clearly as we build our Shared Project code.
Step 3: Add Shared Code Files
We can add our CPP/H files into the root of the project, as well as in a src/ or any folder below this.
In this example we will add in a file at each layer to show how they are combined in the compilation folder:
- Add a file to the root of your Shared Project "SharedProjectRoot.h"
- Switch to folder view and add a new Folder "src", then switch back to solution view
- Add a file to the root of the src/ folder in your Shared Project "SharedProjectSrc.h"
- Switch to folder view and add a new Folder below src, "LED", then switch back to solution view
- Add some CPP and H files to it with the same name e.g. LED.cpp / LED.h
In our LED.h we can include the headers from folders above as well.
The resulting Shared Project folder should look like the one shown in the image above.
Step 4: Add Arduino Project
Now add a new Arduino Project to the Solution.
Add a Reference to the Shared Project (Right Click Arduino Project > Add > Reference)
Ensure it is set as the Startup Project (Right Click Arduino Project > Set as Startup Project)
Now we can include the src/LED/LED.h file into our INO code and use the functions as normal in our INO code.
Step 5: Build and Review
Ensure you have Show all Files selected at this point in the Solution Explorer.
Expand the Arduino Project
Now Build your Arduino Project as normal
We can review the combined build by expanding the Debug or Release folder (depending on configuration set) and see the files combined for compilation (and in the image above)
As you can see using the recommended /src/LED/ folder structure always ensures our code is isolated and is less likely to conflict with another project or library.
Step 6: Find Out More
If the code in your Shared Project is not dependant on the hardware / Arduino you may be able to take advantage of PC Debugging and Unit Testing with Shared Projects as well to save time and reduce issues with the Shared Project Code.