Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Al Williams

Dr. Dobb's Bloggers

Building Arduino

July 21, 2014

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:

	test -d applet || mkdir applet
	echo '#include "Arduino.h"' > applet/$(TARGET).cpp
	cat $(TARGET).ino >> applet/$(TARGET).cpp
	echo 'extern "C" void __cxa_pure_virtual() { while (1) ; }' \ 
             >> applet/$(TARGET).cpp
	cat $(ARDUINO_CORE)/main.cpp >> applet/$(TARGET).cpp

Once you have this level of control over the build process, you could experiment with different techniques, including the assembly language generation I mentioned last time.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

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.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.