Imported Arduino Sketch is bigger in size than what it is in Arduino


When importing an Arduino sketch into Atmel Studio 7, a bunch of unused code also gets imported along. This results in an increase in the size. For an empty sketch, the size difference for the output hex file is (6KB), i.e 3KB in Arduino and 9KB in Atmel Studio. This is not a linear difference, but a constant (to a greater extent). For the "Ethernet Web Server" sketch the size difference is 8KB i.e 32KB in Arduino and 40KB in Atmel Studio. This difference in size have nothing much to do with the version of GCC toolchain or the command line options we pass, but a difference in the build steps involved.

The problem:

The issue is with "implemented but not necessarily used interrupt handlers" in Arduino Core library sources. By deafult Arduino Core library source implement handlers for almost all the vectors and are linked to every sketch. The way the sketch is built in Arduino discards the unused interrupt vector implementation and only pulls in the necessary implementation.

But that is not the case in Atmel Studio. We link all the interrupt handlers even though they are not used. We do use --gc-sections while linking, but that doesn't remove routines that are dependencies for one of the interrupt handlers.

Why Atmel Studio is not able to do this is because of one reason : The build sequence is fixed in Atmel Studio so far and it worked well for all scenarios.

Solution :
The solution is to remove the unused Arduino source library files.