Build Process Driving Me Insane....

No, I would build in-place.

I'm off to a bad start - I can't even get the compiler to run. When I open a cmd shell, and run avr-g++.exe, it complains that it cant find cygwin1.dll. Obviously I'm missing some part or other environment setup. any idea where I can find documentation on the environment used for doing builds?

Regards, Ray L.

You need to open a Bash shell, not a command shell. The Bash shell will have the appropriate paths set up.

There does not appear to be a bash shell in the (limited) cygwin install done by the IDE. So, I installed full Cygwin. Oddly, I can't run ANY of the Arduino tools from it either. No error message - they just do nothing....

Starting to feel really stupid here....

Regards, Ray L.

Can you run make?

I installed gnu make with Cygwin, and it works fine.

Regards, Ray L.

I’ve made up a simple Makefile based on the compiler output of compiling Blink.

#
# Simple Arduino Makefile
#
# Author: Nick Gammon
# Date: 18th March 2015

# where you installed the Arduino app
ARDUINO_DIR = C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/

# various programs
CC = "$(ARDUINO_DIR)hardware/tools/avr/bin/avr-gcc"
CPP = "$(ARDUINO_DIR)hardware/tools/avr/bin/avr-g++"
AR = "$(ARDUINO_DIR)hardware/tools/avr/bin/avr-ar"
OBJ_COPY = "$(ARDUINO_DIR)hardware/tools/avr/bin/avr-objcopy"

MAIN_SKETCH = Blink.cpp

# compile flags for g++ and gcc

# may need to change these
F_CPU = 16000000
MCU = atmega328p

# compile flags
GENERAL_FLAGS = -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=$(MCU) -DF_CPU=$(F_CPU)L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106
CPP_FLAGS = $(GENERAL_FLAGS) -fno-exceptions
CC_FLAGS  = $(GENERAL_FLAGS)

# location of include files
INCLUDE_FILES = "-I$(ARDUINO_DIR)hardware/arduino/cores/arduino" "-I$(ARDUINO_DIR)hardware/arduino/variants/standard"

# library sources
LIBRARY_DIR = "$(ARDUINO_DIR)hardware/arduino/cores/arduino/"

build:
	
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(MAIN_SKETCH) -o $(MAIN_SKETCH).o
	$(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)avr-libc/malloc.c -o malloc.c.o 
	$(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)avr-libc/realloc.c -o realloc.c.o 
	$(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)WInterrupts.c -o WInterrupts.c.o 
	$(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)wiring.c -o wiring.c.o 
	$(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)wiring_analog.c -o wiring_analog.c.o 
	$(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)wiring_digital.c -o wiring_digital.c.o 
	$(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)wiring_pulse.c -o wiring_pulse.c.o 
	$(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)wiring_shift.c -o wiring_shift.c.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)CDC.cpp -o CDC.cpp.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)HardwareSerial.cpp -o HardwareSerial.cpp.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)HID.cpp -o HID.cpp.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)IPAddress.cpp -o IPAddress.cpp.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)main.cpp -o main.cpp.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)new.cpp -o new.cpp.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)Print.cpp -o Print.cpp.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)Stream.cpp -o Stream.cpp.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)Tone.cpp -o Tone.cpp.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)USBCore.cpp -o USBCore.cpp.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)WMath.cpp -o WMath.cpp.o 
	$(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)WString.cpp -o WString.cpp.o 
	rm core.a
	$(AR) rcs core.a malloc.c.o 
	$(AR) rcs core.a realloc.c.o 
	$(AR) rcs core.a WInterrupts.c.o 
	$(AR) rcs core.a wiring.c.o 
	$(AR) rcs core.a wiring_analog.c.o 
	$(AR) rcs core.a wiring_digital.c.o 
	$(AR) rcs core.a wiring_pulse.c.o 
	$(AR) rcs core.a wiring_shift.c.o 
	$(AR) rcs core.a CDC.cpp.o 
	$(AR) rcs core.a HardwareSerial.cpp.o 
	$(AR) rcs core.a HID.cpp.o 
	$(AR) rcs core.a IPAddress.cpp.o 
	$(AR) rcs core.a main.cpp.o 
	$(AR) rcs core.a new.cpp.o 
	$(AR) rcs core.a Print.cpp.o 
	$(AR) rcs core.a Stream.cpp.o 
	$(AR) rcs core.a Tone.cpp.o 
	$(AR) rcs core.a USBCore.cpp.o 
	$(AR) rcs core.a WMath.cpp.o 
	$(AR) rcs core.a WString.cpp.o 
	$(CC) -Os -Wl,--gc-sections -mmcu=$(MCU) -o $(MAIN_SKETCH).elf $(MAIN_SKETCH).o core.a -lm 
	$(OBJ_COPY) -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 $(MAIN_SKETCH).elf $(MAIN_SKETCH).eep 
	$(OBJ_COPY) -O ihex -R .eeprom $(MAIN_SKETCH).elf $(MAIN_SKETCH).hex

I don’t know if it will be preserved, but every line after the build: line starts with a tab, not just multiple spaces. This tab must be preserved.

I did this with IDE 1.0.6 under Windows (running under Cygwin).

You’ll probably need to change the line “ARDUINO_DIR” to reflect where your Arduino install is.

Now you need to rename Blink.ino to Blink.cpp and add an include for Arduino.h, like this:

#include <Arduino.h>

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

Now, changing to that directory (where you saved Blink.cpp) and typing “make” it should compile, like this:

$ make
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-g++" -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -fno-exceptions "-IC:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/arduino/cores/arduino" "-IC:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/arduino/variants/standard" Blink.cpp -o Blink.cpp.o

< lines omitted due to posting limit >

"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-g++" -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -fno-exceptions "-IC:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/arduino/cores/arduino" "-IC:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/arduino/variants/standard" "C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/arduino/cores/arduino/"WString.cpp -o WString.cpp.o
rm core.a
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a malloc.c.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a realloc.c.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a WInterrupts.c.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a wiring.c.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a wiring_analog.c.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a wiring_digital.c.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a wiring_pulse.c.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a wiring_shift.c.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a CDC.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a HardwareSerial.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a HID.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a IPAddress.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a main.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a new.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a Print.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a Stream.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a Tone.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a USBCore.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a WMath.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-ar" rcs core.a WString.cpp.o
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-gcc" -Os -Wl,--gc-sections -mmcu=atmega328p -o Blink.cpp.elf Blink.cpp.o core.a -lm
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 Blink.cpp.elf Blink.cpp.eep
"C:/Documents and Settings/Nick/Desktop/arduino-1.0.6/hardware/tools/avr/bin/avr-objcopy" -O ihex -R .eeprom Blink.cpp.elf Blink.cpp.hex

The resulting .hex file compared exactly to the one created by the IDE.

Now obviously there is work to be done if you want to add more files. But the above should be a nice basis for getting started.

And you could make a lot of improvements about not compiling stuff that doesn't need recompiling, as Make is designed to do. However the above actually produces an object file.

I amended it to replace Blink.cpp throughout with $(MAIN_SKETCH) to make it easier to change to another file.

With your own Makefile you now have the option of passing down your own compiler flags, which will be respected by each module you compile, which is a big win if you want to control things like whether debugging is on or not.

I should point out that compiling is really quite simple. In that build file I set up some variables (like where the compiler is) to save repeating it on every line.

Having done that:

  • Compile everything with avr-gcc or avr-g++ as required
  • Combine all the libraries into one file: core.a
  • Link the main sketch with the combined libraries (giving a .elf file) - the linker will omit unused modules
  • Produce a .eep file (whatever that is, presumably for the EEPROM)
  • Produce a .hex file - for uploading

Nick,

I have several sample make files I'm sure I could hack up, if I could get the Arduino tools to work at all...

I'm amazed there doesn't seem to be hardly any information out there on the environment necessary to make the tools work. Most of what little I've found is from 2008 or so, so very out of date.

Regards, Ray L.

What I showed there should work.

Nick,

Sorry, I missed that whole post!

It still tells me command not found for g++:

$ make Blink.o
g++    -c -o Blink.o Blink.cpp
make: g++: Command not found
<builtin>: recipe for target 'Blink.o' failed
make: *** [Blink.o] Error 127

Something is missing in my environment…

Regards,
Ray L.

If you look at reply #38 you will see that I typed "make" not "make Blink.o".

If I type "make Blink.o" I get the error messages you got. Don't do that.

Typing "make" invokes "make" which reads "Makefile" by default.

Just to clarify, you need to save what I posted under the name "Makefile" - spelt exactly like that.

It looks to me like you didn't install g++. However that doesn't matter as you need to use avr-g++ anyway, which the Makefile does, provided you amend the directory I mentioned to point to your install directory.

Getting close:

$ make
""C:/Program Files (x86)/Arduino-1.5.8/"hardware/tools/avr/bin/avr-g++" -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -fno-exceptions "-I"C:/Program Files (x86)/Arduino-1.5.8/"hardware/arduino/cores/arduino" "-I"C:/Program Files (x86)/Arduino-1.5.8/"hardware/arduino/variants/standard" Blink.cpp -o Blink.cpp.o
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `""C:/Program Files (x86)/Arduino-1.5.8/"hardware/tools/avr/bin/avr-g++" -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -fno-exceptions "-I"C:/Program Files (x86)/Arduino-1.5.8/"hardware/arduino/cores/arduino" "-I"C:/Program Files (x86)/Arduino-1.5.8/"hardware/arduino/variants/standard" Blink.cpp -o Blink.cpp.o'
Makefile:36: recipe for target 'build' failed
make: *** [build] Error 1

Regards, Ray L.

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” button above the posting area (It looks like a scroll with < > inside it).

Not quote tags.

Please post your amended Makefile. In code tags.

OK, had some quoting issues on the install path. I think that's sorted, but the build is failing:

RayL@RayL-Asus /cygdrive/c/Users/RayL/Documents/Arduino/Buildtest
$ make
"C:/Program Files (x86)/Arduino 1.5.8/hardware/tools/avr/bin/avr-g++" -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -fno-exceptions "-IC:/Program Files (x86)/Arduino 1.5.8/hardware/arduino/cores/arduino" "-IC:/Program Files (x86)/Arduino 1.5.8/hardware/arduino/variants/standard" Blink.cpp -o Blink.cpp.o
Makefile:36: recipe for target 'build' failed
make: *** [build] Error 127

RayL@RayL-Asus /cygdrive/c/Users/RayL/Documents/Arduino/Buildtest
$

Here's my Makefile:

#
# Simple Arduino Makefile
#
# Author: Nick Gammon
# Date: 18th March 2015

# where you installed the Arduino app
ARDUINO_DIR = C:/Program Files (x86)/Arduino 1.5.8/

# various programs
CC = "$(ARDUINO_DIR)hardware/tools/avr/bin/avr-gcc"
CPP = "$(ARDUINO_DIR)hardware/tools/avr/bin/avr-g++"
AR = "$(ARDUINO_DIR)hardware/tools/avr/bin/avr-ar"
OBJ_COPY = "$(ARDUINO_DIR)hardware/tools/avr/bin/avr-objcopy"

MAIN_SKETCH = Blink.cpp

# compile flags for g++ and gcc

# may need to change these
F_CPU = 16000000
MCU = atmega328p

# compile flags
GENERAL_FLAGS = -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=$(MCU) -DF_CPU=$(F_CPU)L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106
CPP_FLAGS = $(GENERAL_FLAGS) -fno-exceptions
CC_FLAGS  = $(GENERAL_FLAGS)

# location of include files
INCLUDE_FILES = "-I$(ARDUINO_DIR)hardware/arduino/cores/arduino" "-I$(ARDUINO_DIR)hardware/arduino/variants/standard"

# library sources
LIBRARY_DIR = "$(ARDUINO_DIR)hardware/arduino/cores/arduino/"

build:
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(MAIN_SKETCH) -o $(MAIN_SKETCH).o
    $(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)avr-libc/malloc.c -o malloc.c.o 
    $(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)avr-libc/realloc.c -o realloc.c.o 
    $(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)WInterrupts.c -o WInterrupts.c.o 
    $(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)wiring.c -o wiring.c.o 
    $(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)wiring_analog.c -o wiring_analog.c.o 
    $(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)wiring_digital.c -o wiring_digital.c.o 
    $(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)wiring_pulse.c -o wiring_pulse.c.o 
    $(CC) $(CC_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)wiring_shift.c -o wiring_shift.c.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)CDC.cpp -o CDC.cpp.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)HardwareSerial.cpp -o HardwareSerial.cpp.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)HID.cpp -o HID.cpp.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)IPAddress.cpp -o IPAddress.cpp.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)main.cpp -o main.cpp.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)new.cpp -o new.cpp.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)Print.cpp -o Print.cpp.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)Stream.cpp -o Stream.cpp.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)Tone.cpp -o Tone.cpp.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)USBCore.cpp -o USBCore.cpp.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)WMath.cpp -o WMath.cpp.o 
    $(CPP) $(CPP_FLAGS) $(INCLUDE_FILES) $(LIBRARY_DIR)WString.cpp -o WString.cpp.o 
    rm core.a
    $(AR) rcs core.a malloc.c.o 
    $(AR) rcs core.a realloc.c.o 
    $(AR) rcs core.a WInterrupts.c.o 
    $(AR) rcs core.a wiring.c.o 
    $(AR) rcs core.a wiring_analog.c.o 
    $(AR) rcs core.a wiring_digital.c.o 
    $(AR) rcs core.a wiring_pulse.c.o 
    $(AR) rcs core.a wiring_shift.c.o 
    $(AR) rcs core.a CDC.cpp.o 
    $(AR) rcs core.a HardwareSerial.cpp.o 
    $(AR) rcs core.a HID.cpp.o 
    $(AR) rcs core.a IPAddress.cpp.o 
    $(AR) rcs core.a main.cpp.o 
    $(AR) rcs core.a new.cpp.o 
    $(AR) rcs core.a Print.cpp.o 
    $(AR) rcs core.a Stream.cpp.o 
    $(AR) rcs core.a Tone.cpp.o 
    $(AR) rcs core.a USBCore.cpp.o 
    $(AR) rcs core.a WMath.cpp.o 
    $(AR) rcs core.a WString.cpp.o 
    $(CC) -Os -Wl,--gc-sections -mmcu=$(MCU) -o $(MAIN_SKETCH).elf $(MAIN_SKETCH).o core.a -lm 
    $(OBJ_COPY) -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 $(MAIN_SKETCH).elf $(MAIN_SKETCH).eep 
    $(OBJ_COPY) -O ihex -R .eeprom $(MAIN_SKETCH).elf $(MAIN_SKETCH).hex

Regards, Ray L.