Al Williams

Dr. Dobb's Bloggers

Building Arduino

It is tempting to think of the Arduino IDE as just a text editor that calls the compiler on your behalf, but it does a lot more than that.

Last time, I talked about examining the assembly language output from gcc and showed an example of the output from both a Linux compiler and an AVR compiler like the one that the Arduino uses.

This led to the inevitable question: How can I get assembly output if I'm using the Arduino IDE? As far as I know, the answer is: you can't. At least, not directly. It is tempting to think of the Arduino IDE as just a text editor that calls the compiler on your behalf, but it does a lot more than that.

In an effort to hide a little complexity, the Arduino IDE cretes a lot more complexity, as described in the official documentation.

When you build an Arduino sketch (forgive me if I most often call it a program), the IDE merges all of your files into one big file. It puts an include file along with a bunch of prototypes for any functions you've written. The parse isn't very detailed, so if you use C++ features like default arguments or namespaces, you can expect this to break.

Once this file is complete, the IDE uses the specified board definition to elaborate an environment and call the avr-gcc compiler. After a successful build, it uses avrdude to upload the generated hex file.

You can see the actual compile steps if you open the Arduino's File | Preferences menu and select verbose compilation, but that doesn't really help you get assembly output or set other compiler options.

It stands to reason that anything the IDE can do, you can do too. There are a few alternate build tools for Arduino (I've talked about the Eclipse plug-in before). There is a good makefile available and that's probably a good starting point for changing compiler options without much fuss.

To use the makefile, you do have to modify the template a bit (the instructions are in the initial comments of the file). You can add additional compiler flags to the CEXTRA environment variable, or modify specific rules.

If you dig into the template, you can see the simple preprocessing it does to your code. I notice it doesn't build the function prototypes for you, so I assume if you use your own functions (outside of the Arduino-defined ones) you have to prototype them yourself, which isn't a bad idea anyway. Here's the preprocesing code:

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!