Sharing Arduino Code Between Projects

by Visual Micro 27. May 2016 18:15

NOTE: We recommend using shared projects for c++/.h code.

related: share, edit and debug libraries between projects

It's a popular request to share code between Arduino projects. To share code just as if it has been added directly to the project as opposed to using a seperate library.

In Visual Studio use the "Add Existing" options to add existing code from an alternative location. NOTE: Click the "Add as link" option when adding the code.

In more recent IDE's such as Vs2017/2019 use "Add Existing Item" which automatically creates the link to the source.

You should see a link to the source is added to the current project. #include linked code as if it exists in the project folder (without path qualifier).

Below shows #include "UID_DS2401.h" has been included in the sketch code although it exists in an entirely different location ('up two parent folders\Test2').

Note: When the project is compiled any linked sources are copied into the hidden build folder along with the other project sources. This means that a name conflict could occurr therefore all the project sources should have unique names. This method of compile keeps things as close to normal "Arduino code" as possible and should future need arise, the linked sources can be copied locally and then compiled with the Arduino Ide without modifiying the source code.