11

I have started to play with and arduino UNO quite recently (without any prior experience with micro-controllers). I would like to use emacs instead of the IDE, and I'd also like to know what the IDE does under the hood, in order to be able to write my own makefile. The tutorials I've found are either outdated, or are presented as a series of steps without any explanation. I'd appreciate it if someone could explain to me how the whole compliation/linking/upload process works using gcc-avr and avr-dude, and how it is used by the IDE.

2
  • 5
    You can take a look under the hood by "Show verbose output during: ☑ compilation ☑ upload" Under File → Preferences.
    – jippie
    Apr 27 '14 at 17:47
  • Almost duplicate: Compiling code via terminal. See the links to arduino-builder and Sudar Muthu' Makefile in my answer to that question. Mar 31 '17 at 8:10
13

If you want an exact duplication of what the IDE does but want it driven from the command line, that's what Ino is for. The full Arduino build process involves copying a lot of files from a lot of places, and is generally not trivial to duplicate.

If you're ready to let go of .ino files and the Arduino libraries, you get a much simpler toolset. avr-gcc compiles, avrdude uploads, and you're done. Here's one of my makefiles from a simple project:

CC=avr-gcc
CXX=avr-c++
CXXFLAGS=-Wall -Wextra -mmcu=atmega1284p -Os
CFLAGS=$(CXXFLAGS)
BINARY=ledmatrix
OBJECTS=

all: $(BINARY)
↹@avr-size $<

$(BINARY): $(OBJECTS)

clean:
↹@rm -f $(BINARY) $(BINARY).hex $(OBJECTS)

upload: $(BINARY).hex
↹@avrdude -c usbasp -p m1284p -U flash:w:$<:i

%.hex: %
↹@avr-objcopy -j .text -j .data -O ihex $< $@

.PHONY: all clean upload

If copying-and-pasting, be sure to replace all "↹" with tab characters.

EDIT:

I have created a repository with my buildsystem on Github.

2
  • Thanks, your makefile is pretty much what i was trying to come up with. But i guess getting rid of the libraries means that i wont be able to use functions such as digitalWrite()? In that case, where should i look to learn their plain C equivalents?
    – Ash
    Apr 27 '14 at 18:06
  • 1
    The Arduino libraries would contain their equivalents, but they can be a bit erudite to read. I recommend finding a low-level AVR tutorial to work through that will explain to you the various registers and peripherals. Oh, and the datasheet. Always get the datasheet. Apr 27 '14 at 18:09
0

Here is a high-level overview of Arduino's compilation process, for folks like me who found this post while hunting for one:

The compilation process

The arduino code is actually just plain old c without all the header part (the includes and all). when you press the 'compile' button, the IDE saves the current file as arduino.c in the 'lib/build' directory then it calls a makefile contained in the 'lib' directory.

This makefile copies arduino.c as prog.c into 'lib/tmp' adding 'wiringlite.inc' as the beginning of it. this operation makes the arduino/wiring code into a proper c file (called prog.c).

After this, it copies all the files in the 'core' directory into 'lib/tmp'. these files are the implementation of the various arduino/wiring commands adding to these files adds commands to the language

The core files are supported by pascal stang's procyon avr-lib that is contained in the 'lib/avrlib' directory

At this point the code contained in lib/tmp is ready to be compiled with the c compiler contained in 'tools'. If the make operation is succesfull then you'll have prog.hex ready to be downloaded into the processor.

https://www.arduino.cc/en/Main/Documentation

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.