Atmel Studio for Arduino-guys - Part1 (continued): precompile Arduino libraries

Hi, today we will talk about precompilation of Arduino libraries in Atmel Studio 6 and how to use precompiled libraries in your projects. You should create and cofigure new project, add required files, compile... and that's all! All steps was described in part 1. But I prepared for you quick action list which you could follow to create precompiled Arduino Servo library. When you will be familiar with steps, you could easily create projects for other libraries like Wire and SPI.
  1. Create new project: Ctrl+Shift+N or right click on solution -> Add -> New project..., select 'GCC C++ Static Library Project' and name it Arduino.Precompiled.Servo. Also in 'Solution:' drop down check that 'Add to solution' item selected.

  2. Remove files automatically added to project: select file Arduino.Precompiled.Servo.cpp in solution explorer and press Del or right click on file and select Remove. If asked, press "Delete" button to delete file permanently.

  3. Add required files to project - in our case add as links 2 files Servo.cpp and Servo.h from (SolutionDir)\arduino-1.0.3\libraries\Servo.

  4. Configure project - Build tab: press right click on project, select Properties, goto Build tab. Set "Artifact Name" to libarduinoservo for all build modes.

  5. Configure project - Build events: add post build event to copy resulting library to external folder. It is optional step, but sometimes useful...
    xcopy "$(OutputDirectory)\$(OutputFileName)$(OutputFileExtension)" "$(SolutionDir)$(SolutionName).$(Configuration)\" /f /y

  6. Configure project - Toolchain - AVR\GNU C Compiler: as we have only cpp files in our project, we could skip configuration of AVR\GNU C Compiler.

  7. Configure project - Toolchain - AVR\GNU C++ Compiler - Symbols: (both build modes)
    1. F_CPU=16000000L
    2. ARDUINO=103
    3. USB_VID=null
    4. USB_PID=null

  8. Configure project - Toolchain - AVR\GNU C++ Compiler - Directories: (both build modes)
    1. ../../../arduino-1.0.3/hardware/arduino/variants/mega
    2. ../../../arduino-1.0.3/hardware/arduino/cores/arduino

  9. Configure project - Toolchain - AVR\GNU C++ Compiler - Optimization:
    1. Release build mode: in Other flags textbox add text '-fdata-sections', and check Prepare functions for garbage collection
    2. Debug build mode: no changes

  10. Configure project - Toolchain - AVR\GNU C++ Compiler - Miscellaneous: in Other flags textbox add text '-fno-exceptions' in both build modes.

  11. Configure project - Toolchain - AVR\GNU Linker - Libraries: in this project we can skip it, but usually you should add name of used libraries to 'Libraries' section and path to folder where libraries located to 'Library search path' section. Name of the library should be added without 'lib' prefix like: arduinocore and not libarduinocore. Also do that in both build modes, or you could use different paths\libraries for different build modes.

  12. Configure project - Toolchain - AVR\GNU Linker - Optimization:
    1. Release build mode: check 'Garbage collect unused sections' and 'Relax Branches'
    2. Debug build modes: skip

  13. Also, sometimes you create projects that have dependencies on libraries produced by other project in same solution. In such case you should correctly setup dependencies to prevent situations when dependent project building before depending. To do this right click on project, select 'Project Dependencies...' and check required project(s), so Atmel Studio will have an ability to calculate correct build order.
You are almost done, just build and check existence of libarduinoservo.a in Arduino.Precompiled.Release folder (or in Arduino.Precompiled.Debug folder, depending on what build mode you used). That's it... You created precompiled Arduino Servo library!

Video below provides quick demo about how to use precompiled Arduino Wire library in Atmel Studio 6: