Coding for AVR II: Writing and Building on Linux

Since we already know how to turn our code into hex in Windows, we should also know how to do it using the best OS around: Linux. Please be aware that while the instructions here have been tested in Linux Mint (Ubuntu/Debian based), they should also work with your favorite distro.

First and foremost, you should write your code in a text file, in your favorite editor (or IDE, if you prefer). For simplicity sake, I’m using the default gedit. After that is done, you should install avr-gcc and it’s dependencies. avr-gcc is the most used c and c+ avr compiler around, and includes some really useful libraries.

sudo apt-get install gcc-avr avr-libc

Now, cd into the dir you’re using. Or just put the c file in your home folder. I’m assuming you have a led.c file to burn in an ATTiny2313. Change this to whatever you’re using. We’re going to start by compiling the code:

avr-gcc -g -Os -mmcu=attiny2313 -c led.c

Now you should have a led.o file in your folder. Let’s link it:

avr-gcc -g -mmcu=attiny2313 -o led.elf led.o

Now you should have a led.elf file in your folder. To finish it off, let’s get the .hex file:

avr-objcopy -j .text -j .data -O ihex led.elf led.hex

And we’re done! We now have our .hex file. We can just go ahead and burn it in our chip using our favorite programmer!

It’s a really straightforward process, but if you have any problems, just post in the comment bellow!


This is now the most seen post in my blog. I’m quite happy to see that some people are trying to start programming with Linux, and as such, I feel best to add the avr-libc to the install command, which has some high usage libraries. Also, for those that want to streamline the building process, I made a barbaric bash script to to so. Download it here. After the download, you should run:

chmod +x build

./build source_file.c mmcu

In the above example, mmcu is attiny2313. This should give you an source_file.hex, ready to go!

This entry was posted in AVR, Electronics, Tutorial and tagged , , , , , , , . Bookmark the permalink.

One Response to Coding for AVR II: Writing and Building on Linux

  1. Pingback: My First AVR Circuit II: Source Code | Electro Sparrow

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.