Arduino Development; There’s a Makefile for That

Hardware and software combined, Arduino does many things right. It lowers the entry level into embedded systems development with a nifty hardware abstraction layer. It aims for cross-platform compatibility by supporting Windows, Mac OSX, and Linux operation systems. It throws out the need for an external programmer to get you up-and-blinkin’ those LEDs quickly.

One thing most of us never cease to curse about, though, is the IDE. Many have cried out wildly against the Java-based text-editor for its cryptic compiling-and-linking process, its inability to accommodate bare C or C++ source files, and (shh!) its lack of Vim keybindings. Fortunately, our cries have been heard, and the like many community-based projects, the community fights back with a custom solution.

Calling all Grumpy Engineers: The Arduino-Makefile

What began as [Sudar’s] lightweight program to escape the IDE has become a fully-blown, feature rich Makefile that has evolved and adapted to grow with the changes of Arduino. With a community of 47 contributors, the Makefile enables you to escape from the IDE entirely by writing code in the cushy text editor of your choice and compiling with a simple incantation of make into your terminal, be you in Linux, Mac, or Windows.

Without further ado, let’s take a walking tour of the project’s highlights.

Cryptic Shortcuts–Begone!

For many beginners, writing (or even editting) a Makefile can cause some serious confusion with most Makefile authors’ shameless shortcut use. Make no mistake, the cryptic syntax of many Makefiles forms a concise list of instructions for compiling and linking your executable, but this recipe does seem a bit hard to parse for the uninitiated. What’s more, Makefiles also tend to throw bizarre errors (trailing whitespace anyone?) that are difficult to track down–especially when we want to spend most of our time bringing up embedded systems, not understanding the mechanics and syntax of a Makefile. Fortunately [Sudar] and the rest of the development team have made the interface very human readable.

Their solution: A two-part Makefile. For a simple project, your Makefile need be no longer than this snippet (inspired from the Makefile Examples):

BOARD_TAG = uno
include $(ARDMK_DIR)/Arduino.mk

Project-specific settings (like which board you’re using) are outlined in this brief Makefile that then includes the larger Makefile which contains the actual nuts and bolts for building your code. The assumption here is that you’ve defined two environment variables, both ARDMK_DIR and ARDUINO_DIR that point towards the (1) Arduino Makefile directory and the (2) Arduino Installation directory. In addition, if you have additional libraries, you can include them with a line in your top-level Makefile that defines the filepath.

USER_LIB_PATH += /home/my_username/my_libraries_directory

You’ll also need to add all libraries you’re using (both user-added and built-in) to the list of libraries like so:

ARDUINO_LIBS += Wire \
SPI \
my_custom_lib

The benefit of a “split-Makefile” setup is that the short, top-level Makefile hides the gritty compiler gymnastics involved in compiling and linking against the default and user-added Arduino Libraries. On the flip-side, this “mini” Makefile becomes a brief, informative summary of a few minor details, such as which Arduino Libraries are being used, that are likely more relevant to the author and future developers.

Raw C and C++ is In–if you prefer such things

Tired of that *.ino file extension? Tired of having to constrain yourself into those setup and loop functions? With the Makefile, you can quickly wish these away and write your code in raw C or C++. You can even neglect to #include <Arduino.h> if you want to work in vanilla C or C++ and disregard the Arduino libraries altogether.

That said, if you’re mixing C and C++, keep in mind that you’ll need to insert guards around your C header files like so:

Adding Project Libraries

Possibly my favorite aspect of the Arduino Makefile is its flexibility to accomodate a richer file structure and painlessly split your project into multiple files. Let’s say I have a one-off project where it makes sense to include a custom library. Given the flexibility of the Makefile, you can define:

If you need more flexibility, you can also split your source code across several directories, keeping the Makefile in the same directory of your code that acts like a “main.” (In this case, main.ino defines setup and loop.) To do so, your Makefile will, instead, have:

USER_LIB_PATH+=../libs
ARDUINO_LIBS += my_custom_library

in your project Makefile, and your directory structure will look like so:

Finally, we don’t need to constrain ourselves to writing just C++ classes to split projects into multiple files. Since we’re really working with vanilla C++, you can freely split your project into multiple source (*.c, *.cpp, *.ino, *.pde) and header (*.h, *.hpp) files that live in the same directory as the Makefile, and the Makefile will compile them into one executable.

Other Micros are Fair Game Too

Finally, the Arduino-Makefile is also compatible with a host of other microcontrollers and programmers, not just the ATMEGA328P on the Uno. In short, this feature is an exposition of the features of AVRDUDE, the program for downloading and uploading code to various AVR microcontrollers. Last, but not least, it’s also Teensy-compatible.

Define Our Standard

If you’re looking to get comfortable with the Arduino-Makefile workflow, have a quick look at their examples directory for a host of different use-cases. You can also take a peek at my i2c_demultiplexing_demo source from a couple weeks back for yet another example. At the end of the day, Arduino, with its giant library collection, makes project prototyping fast. For bigger projects, though, we don’t tend to see any standard practices for file organization to make projects easier to navigate. That’s where you come in. With the flexibility of the Makefile, you get it all: the text editor you always wanted, the separate header and implementation files, a clean directory… Now it’s your shot to take this tool and refine your workflow into a method worth sharing with the rest of us.

Post navigation

32 thoughts on “Arduino Development; There’s a Makefile for That”

Love it. Love everything about it. I started something similar a while back but instead of using the arduino ecosystem I started writing everything from scratch. It was fun, but fool-hearty :Phttps://github.com/pborges/avrlib
in case your curious.

Uhm, well, it’s a Makefile. Nice, but not as grand as you make it sound. And when you’re on the verge of working “in vanilla C or C++ and disregard the Arduino libraries altogether” then you might as well use avr-gcc in the first place (for which similar Makefiles already exist).

I agree! I think that if you do not like arduino IDE then just move on to avr-gcc. It’s the next logical step to take and opens up a lot more to whomever decides to go down that path. Although there is a bit of a learning curve and you need to get a programmer, this is just the nature of the beast if you want to keep down the path. The Arduino IDE is for beginners and quick nonsense projects in my oppinion. If you are an advanced programmer then play with the big boy toys.

If one is working on windows platform it is even simpler to work with Atmel studio. Now version 7 is available.
The Atmel AVRISP MkII programmer is cheap and dispense of a boot loader that take space in flash.

I wrote some ‘commercial’ arduino code (used in some projects that you can build) and while I’m ok at doing makefiles (my project did use them about 3 or 4 yrs ago when I started) my target audience of ‘makers’ will usually not understand the makefile idea and they need the IDE.

this is what I like about arduino; I can live with the cli and emacs and gcc and makefiles; but others can just load the ino/cpp files into the IDE and have at it. everyone can be happy.

It took me a long time to get into the whole Arduino thing, because of the uhmmm… let’s call it “lacking” IDE.

But then I found out about VisualMicro which just lets me use Visual Studio 2010 or later, which I also use for work everyday. And other plugins for VisualStudio such as WholeTomato VisualAssistX really help to make Arduino programming fun. Not to mention the fact that VisualMicro is smart enough to do little things like close the serial terminal when it needs to upload a sketch, and reopen it afterwards.

I like command line tools a lot, and I understand how a Makefile can make things easier for scripting things, but for the Arduino audience (i.e. beginning programmers) I think Atmel Studio or Visual Studio with VisualMicro is the way to go.

yeah this is over two years late, but I have recently started using Visual Micro inside of VS Community edition 2015 (and now 2017)… and it’s pretty sweet. Code completion, definitions, source control… nice.

I am using a similar build script for the Teensy-3.1 in my electronic drum project… I am not a fan of the Arduino IDE or framework, but Paul has invested tons of time to get a very simple real time audio library for the Teensy, and I am using that heavily. I have my own libraries for other things, and don’t have a single .ino file, but still get the advantages of powerful audio routines without needing to write it all myself. Best of both worlds!

Neat.
Now you just need to put these instructions back into the arduino-makefile README, or at least a TUTORIAL file in the repo.

Even then, it’s kind of a roundabout way to go. Lots of settings, paths, files, etc to all manage. Again, we are confronted with the paradox of the Arduino IDE: It is a terrible piece of junk, but it is very easy to use, and to get something working. It still emulates the odd compile path of building up the core.a archive, then linking it at the end.

One of these days, I am just going to make an Arudino veneer on top of NetBeans. Regular old NetBeans. MicroChip uses it for their whole IDE, and other than the slowness, it is actually quite good.
Yes, an Arduino plug for netBeans already exists, but it’s a pretty horrible roundabout thing.
Someone could just roll a whole ArudinoBeans package, with support for just a simple “New Arduino Project”, all the examples, etc.

What would be pretty awesome is having the ability to use Arduino libraries in an AVR development setup (Atmel Studio). Sure you’ll have to be aware of the various hardware resources used by the various libraries but it would mean that you can easily leverage the hundreds of Arduino Libraries in your code without having to be tied to the limited Arduino IDE and the ‘setup, loop’ structure.

And this is possible. Simply create a static library containing all the Arduino Core libraries. Be sure to comment out the main function and setup and loop function prototypes in main.cpp. Additional libraries can be included as source…at least this is what the Arduino IDE does behind the scenes.

There is a plugin for that for Atmel Studio. Unfortunately, Atmel Studio is a tricky mishmash of stuff with a mob-brawl of community supported featured. And it’s bizarrely slow, despite being based on the Visual Studio, which is usually pretty great.

Say what you like .The great benefit of the Arduino IDE is that people with no knowledge of microprocessors can follow simple instructions and have some leds blinking in an hour on a new board they can buy for less than $3 -the Arduino Pro ,and then develop their skills from there . Its a lead in to greater things of which a makefile might be considered one.

Arduino has launched a new project called “Arduino Builder” which is a command line binary tool to compile Arduino code.
It is currently in beta but very soon it will replace the Java builder included in the IDE.

It takes care of a lot of steps like automatic protype generation and dealing with libraries and dependencies

If you want to use your IDE of choice but wanto to make sure yoy’re 100% compatible with the Arduino IDE this is the tool you should be using (it also supports all the different implementations of Arduino provided by the community)

An Arduino sketch differs from a standard C program in that it misses a main (provided by the Arduino core), function prototypes are not mandatory, and libraries inclusion is automagic (you just have to #include them). This tool generates function prototypes and gathers library paths, providing gcc with all the needed -I params.”

that’s a typical arduino “my way or the highway” response that’s not very supportive of an open approach to software development, you’ll be recommending the closed-source single platform atmel studio next, oh wait….

I did find a command line option though. Open your arduino sketch and compile with pref verbose on. Then copy the second arduino-builder line from the console and paste into a file named ‘go’ in your sketch directory. minimize the arduino IDE window (if you close the IDE it will remove the temporary build directories in /tmp). If you want, while editing go, remove the ‘-verbose’ arg.

Now I just edit my sketch ino file in vi and run go to compile. You can also copy the upload command from the console to go file to upload on compile success.

This solution is simple and avoids makefile hacking and all the stuff required to compile arduino sketches from command line. BTW Sudar’s solution works great on atmega328 compiles. I’m not knocking it at all. I’ve used it or other predecessors for years now.