# Programming for the Arduino without the Arduino IDE.. but with the provided libraries?

I recently started a new project using my Arduino that's been collecting dust for a while. Along with the physical board collecting dust, so has my copy of avr-gcc and the Arduino libraries. I can manage updating avr-gcc, but I can't remember how I compiled the Arduino libraries. With the Arduino source, there is listed source files and such, but no makefile (that I can see) for actually building the library. I don't care about building the IDE, I just want the library in binary format and the header files that I'll need. Is there any documentation or such for doing this?

I don't want to use the Arduino IDE, I prefer using my own text editor and a makefile.

-
This may be helpful to you. –  arminb Mar 22 '12 at 2:26
@arminb bleaklow.com/2010/06/04/a_makefile_for_arduino_sketches.html would be more useful than that. Your link just gives a way to build sketches from the command line, not a way to build the arduino library. Irrelevant either way though because I've answered my own question –  Earlz Mar 22 '12 at 19:40
hardwarefun.com/tutorials/… –  jippie Dec 15 '13 at 8:31
@jippie I looked at that(not when asked, but recently when revisiting this) but was put off by the fact that you have to have the Arduino IDE installed. Kind of defeats the purpose –  Earlz Dec 15 '13 at 9:01

I have created a little project with a custom build system (using Ruby) that makes this pretty easy without having to install the Arduino IDE. Basically, it uses a template Makefile, and a ruby script to make compiling the Arduino libraries extremely easy. You can see it at https://github.com/Earlz/make-wiring

However, I'm leaving the old answer here for information on rolling your own. It's quite cumbersome and annoying though:

Directions:

2. Copy the contents of hardware/arduino/cores/arduino to a new directory I'll refer to as arduino_build
3. Copy the pins_arduino.h file from whichever Arduino variant is yours from hardware/arduino/variants (check boards.txt if you're not sure) to arduino_build
4. Add this makefile to arduino_build:

.

#BSD licensed, see http://lastyearswishes.com/static/Makefile for full license

HDRS = Arduino.h binary.h Client.h HardwareSerial.h IPAddress.h new.h pins_arduino.h Platform.h Printable.h Print.h \
Server.h Stream.h Udp.h USBAPI.h USBCore.h USBDesc.h WCharacter.h wiring_private.h WString.h

OBJS = WInterrupts.o wiring_analog.o wiring.o wiring_digital.o wiring_pulse.o wiring_shift.o CDC.o HardwareSerial.o \
HID.o IPAddress.o main.o new.o Print.o Stream.o Tone.o USBCore.o WMath.o WString.o

#may need to adjust -mmcu if you have an older atmega168
#may also need to adjust F_CPU if your clock isn't set to 16Mhz
CFLAGS = -I./ -std=gnu99  -DF_CPU=16000000UL -Os -mmcu=atmega328p
CPPFLAGS = -I./ -DF_CPU=16000000UL -Os -mmcu=atmega328p

CC=avr-gcc
CPP=avr-g++
AR=avr-ar

default: libarduino.a

libarduino.a:   ${OBJS}${AR} crs libarduino.a $(OBJS) .c.o:${HDRS}
${CC}${CFLAGS} -c $*.c .cpp.o:${HDRS}
${CPP}${CPPFLAGS} -c $*.cpp clean: rm -f${OBJS} core a.out errs

install: libarduino.a
mkdir -p ${PREFIX}/lib mkdir -p${PREFIX}/include
cp *.h ${PREFIX}/include cp *.a${PREFIX}/lib


And then just run

make
make install PREFIX=/usr/arduino (or whatever)


And then to make use of the compiled libraries and such you can use a simple makefile like this:

default:
avr-g++ -L/usr/arduino/lib -I/usr/arduino/include -Wall -DF_CPU=16000000UL -Os -mmcu=atmega328p -o main.elf main.c -larduino
avr-objcopy -O ihex -R .eeprom main.elf out.hex
avrdude -c arduino -p m328p -b 57600 -P /dev/ttyUSB0 -U flash:w:out.hex



Also, if you try to compile the libraries in libraries/ you'll get a linker error if you don't do things in the right order. For instance, I had to do this to use SoftwareSerial:

    avr-g++ -L/usr/arduino/lib -I/usr/arduino/include -Wall -DF_CPU=16000000UL -Os -mmcu=atmega328p -o main.elf main.c -lSoftwareSerial -larduino


The -larduino must be the last library on the command line

Anyway, this was a pretty easy way to compile it for me. As future versions of the Ardunio come out, this makefile should be fairly future-proof, requiring just a few modifications to OBJS and HDRS. Also, this makefile should work with both BSD make and GNU make

See also a slightly modified version of this answer on my blog with an already compiled binary of the library (compiled using the "standard" pins_arduino.h).

** EDIT **

I found that adding the following compiler optimization flags to both the library building Makefile and each individual project Makefile greatly reduces the size of the final compiled binary. This makes the final binary size comparable to that of the IDE.

-Wl,--gc-sections -ffunction-sections  -fdata-sections


.

So, for the library build makefile:

CFLAGS = -I./ -std=gnu99  -DF_CPU=16000000UL -Os -Wl,--gc-sections -ffunction-sections  -fdata-sections -mmcu=atmega328p
CPPFLAGS = -I./ -DF_CPU=16000000UL -Os -Wl,--gc-sections -ffunction-sections  -fdata-sections -mmcu=atmega328p


and, for each project makefile:

avr-g++ -L/usr/arduino/lib -I/usr/arduino/include -Wall -DF_CPU=16000000UL -Os -Wl,--gc-sections -ffunction-sections  -fdata-sections -mmcu=atmega328p -o main.elf main.c -larduino


.

-

If you're willing to use the Arduino IDE once (or once per device-type), it's the easiest way to build a static library, as well as getting the library sources. After that you can use whatever development tools suit you.

This Arduino article (written for users moving to the Eclipse IDE) describes building the Arduino library by compiling a sketch with the Arduino IDE and retrieving the library from Arduino's temporary directory. Scroll down about 1/4 of the page to the section

1. Copying the library from an Arduino IDE project
-