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

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:

  1. Download a copy of the Arduino IDE source code
  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
upload:
    avrdude -c arduino -p m328p -b 57600 -P /dev/ttyUSB0 -U flash:w:out.hex

all: default upload

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

.

Ref: http://arduino.cc/forum/index.php?topic=153186.0


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