Ubuntu 14.04 x64 + Netbeans 8.02 + Arduino 1.06 = not working [SOLVED]

OS: Ubuntu 14.04 x64 IDE: Netbeans 8.02 (x64) Java: OpenJDK 1.7.0.65 Arduino: 1.06 Hardware: Official Mega2560

I followed these instructions on how to get Arduino working in Netbeans: http://playground.arduino.cc/Code/Netbeans

I have successfully compile the core Arduino library in both Debug and Release versions. I can compile the 'blink' program and link it in Debug or Release. The program appears to even upload to the Arduino hardware.

Problem? The program doesn't take/run on the Arduino hardware. However, if I compile/upload the exact same 'blink' program from the Arduino IDE, the hardware runs the program fine and the LED blinks. How can I tell? I make the Arduino IDE version blink at 1000ms and the Netbeans blink at 250ms.

I turned on the verbose features in the Arduino IDE and tried to make sure the Netbeans compiler/linker options matched the Arduino options but i still can't get a Netbeans compiled program to run on the hardware.

Now I am just frustrated and I am pretty sure I can't see the forest through the trees. Perhaps someone with a fresh view can see where I am going wrong. Here is my Makefile:

# Environment
MKDIR=mkdir
CP=cp
CCADMIN=CCadmin

# upload
upload: .build-post
    /usr/local/arduino/hardware/tools/avrdude -C/usr/local/arduino/hardware/tools/avrdude.conf -patmega2560 -cwiring -P/dev/ttyACM0 -b115200 -D -F -V -Uflash:w:${CND_ARTIFACT_PATH_${CONF}}.hex:i

# build
build: .build-post

.build-pre:
# Add your pre 'build' code here...

.build-post: .build-impl
# Add your post 'build' code here...
    #/usr/local/arduino/hardware/tools/avr/bin/avr-objcopy -O ihex ${CND_ARTIFACT_PATH_${CONF}} ${CND_ARTIFACT_PATH_${CONF}}.hex
    /usr/local/arduino/hardware/tools/avr/bin/avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 ${CND_ARTIFACT_PATH_${CONF}} ${CND_ARTIFACT_PATH_${CONF}}.hex

    /usr/local/arduino/hardware/tools/avr/bin/avr-size --mcu=atmega2560 -C ${CND_ARTIFACT_PATH_${CONF}}

# clean
clean: .clean-post

.clean-pre:
# Add your pre 'clean' code here...

.clean-post: .clean-impl
# Add your post 'clean' code here...


# clobber
clobber: .clobber-post

.clobber-pre:
# Add your pre 'clobber' code here...

.clobber-post: .clobber-impl
# Add your post 'clobber' code here...


# all
all: .all-post

.all-pre:
# Add your pre 'all' code here...

.all-post: .all-impl
# Add your post 'all' code here...


# build tests
build-tests: .build-tests-post

.build-tests-pre:
# Add your pre 'build-tests' code here...

.build-tests-post: .build-tests-impl
# Add your post 'build-tests' code here...


# run tests
test: .test-post

.test-pre: build-tests
# Add your pre 'test' code here...

.test-post: .test-impl
# Add your post 'test' code here...


# help
help: .help-post

.help-pre:
# Add your pre 'help' code here...

.help-post: .help-impl
# Add your post 'help' code here...

# include project implementation makefile
include nbproject/Makefile-impl.mk

# include project make variables
include nbproject/Makefile-variables.mk

Here is a sample output from the static library compilation:

"/usr/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .clean-conf
make[1]: Entering directory `/d01/dev/Arduino/arduino_corelib'
rm -f -r build/Release
rm -f dist/Release/Arduino-Linux-x86/libarduino_corelib.a
make[1]: Leaving directory `/d01/dev/Arduino/arduino_corelib'

CLEAN SUCCESSFUL (total time: 51ms)
"/usr/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/d01/dev/Arduino/arduino_corelib'
"/usr/bin/make"  -f nbproject/Makefile-Release.mk dist/Release/Arduino-Linux-x86/libarduino_corelib.a
make[2]: Entering directory `/d01/dev/Arduino/arduino_corelib'

mkdir -p build/Release/Arduino-Linux-x86/_ext/1807441932
rm -f "build/Release/Arduino-Linux-x86/_ext/1807441932/CDC.o.d"
/usr/local/arduino/hardware/tools/avr/bin/avr-g++ -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega2560 -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -fno-exceptions   -c -O2 -I/usr/local/arduino/hardware/tools/avr/lib/gcc/avr/4.3.2/include -I/usr/local/arduino/hardware/tools/avr/lib/gcc/avr/4.3.2/include-fixed -I/usr/local/arduino/hardware/tools/avr/lib/avr/include -I/usr/local/arduino/hardware/arduino/cores/arduino -I/usr/local/arduino/hardware/arduino/variants/standard -MMD -MP -MF "build/Release/Arduino-Linux-x86/_ext/1807441932/CDC.o.d" -o build/Release/Arduino-Linux-x86/_ext/1807441932/CDC.o /usr/local/arduino/hardware/arduino/cores/arduino/CDC.cpp

mkdir -p build/Release/Arduino-Linux-x86/_ext/1807441932
rm -f "build/Release/Arduino-Linux-x86/_ext/1807441932/WInterrupts.o.d"
/usr/local/arduino/hardware/tools/avr/bin/avr-gcc -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega2560 -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106   -c -O2 -I/usr/local/arduino/hardware/tools/avr/lib/gcc/avr/4.3.2/include -I/usr/local/arduino/hardware/tools/avr/lib/gcc/avr/4.3.2/include-fixed -I/usr/local/arduino/hardware/tools/avr/lib/avr/include -I/usr/local/arduino/hardware/arduino/cores/arduino -I/usr/local/arduino/hardware/arduino/variants/standard -MMD -MP -MF "build/Release/Arduino-Linux-x86/_ext/1807441932/WInterrupts.o.d" -o build/Release/Arduino-Linux-x86/_ext/1807441932/WInterrupts.o /usr/local/arduino/hardware/arduino/cores/arduino/WInterrupts.c
mkdir -p build/Release/Arduino-Linux-x86/_ext/1807441932

a - build/Release/Arduino-Linux-x86/_ext/1807441932/CDC.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/HID.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/HardwareSerial.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/IPAddress.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/Print.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/Stream.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/Tone.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/USBCore.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/WInterrupts.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/WMath.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/WString.o
a - build/Release/Arduino-Linux-x86/_ext/666176297/malloc.o
a - build/Release/Arduino-Linux-x86/_ext/666176297/realloc.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/main.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/new.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/wiring.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/wiring_analog.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/wiring_digital.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/wiring_pulse.o
a - build/Release/Arduino-Linux-x86/_ext/1807441932/wiring_shift.o
/usr/local/arduino/hardware/tools/avr/bin/avr-ranlib dist/Release/Arduino-Linux-x86/libarduino_corelib.a
make[2]: Leaving directory `/d01/dev/Arduino/arduino_corelib'
make[1]: Leaving directory `/d01/dev/Arduino/arduino_corelib'

BUILD SUCCESSFUL (total time: 1s)

And this is the output from the 'blink' program:

"/usr/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .clean-conf
make[1]: Entering directory `/d01/dev/Arduino/blink'
rm -f -r build/Release
rm -f dist/Release/Arduino-Linux-x86/blink
make[1]: Leaving directory `/d01/dev/Arduino/blink'

CLEAN SUCCESSFUL (total time: 53ms)
"/usr/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/d01/dev/Arduino/blink'
"/usr/bin/make"  -f nbproject/Makefile-Release.mk dist/Release/Arduino-Linux-x86/blink
make[2]: Entering directory `/d01/dev/Arduino/blink'
mkdir -p build/Release/Arduino-Linux-x86
rm -f "build/Release/Arduino-Linux-x86/main.o.d"
/usr/local/arduino/hardware/tools/avr/bin/avr-g++ -Os -Wl,--gc-sections -mmcu=atmega2560   -c -O2 -I/usr/local/arduino/hardware/tools/avr/lib/gcc/avr/4.3.2/include -I/usr/local/arduino/hardware/tools/avr/lib/gcc/avr/4.3.2/include-fixed -I/usr/local/arduino/hardware/tools/avr/lib/avr/include -I/usr/local/arduino/hardware/arduino/cores/arduino -I/usr/local/arduino/hardware/arduino/variants/standard -MMD -MP -MF "build/Release/Arduino-Linux-x86/main.o.d" -o build/Release/Arduino-Linux-x86/main.o main.cpp
mkdir -p dist/Release/Arduino-Linux-x86
/usr/local/arduino/hardware/tools/avr/bin/avr-g++ -o dist/Release/Arduino-Linux-x86/blink build/Release/Arduino-Linux-x86/main.o -L../arduino_corelib/dist/Debug/Arduino-Linux-x86 -larduino_corelib -lm -Os -Wl,--gc-sections -mmcu=atmega328p
make[2]: Leaving directory `/d01/dev/Arduino/blink'
make[1]: Leaving directory `/d01/dev/Arduino/blink'
#/usr/local/arduino/hardware/tools/avr/bin/avr-objcopy -O ihex dist/Release/Arduino-Linux-x86/blink dist/Release/Arduino-Linux-x86/blink.hex
/usr/local/arduino/hardware/tools/avr/bin/avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 dist/Release/Arduino-Linux-x86/blink dist/Release/Arduino-Linux-x86/blink.hex
/usr/local/arduino/hardware/tools/avr/bin/avr-size --mcu=atmega2560 -C dist/Release/Arduino-Linux-x86/blink
AVR Memory Usage
----------------
Device: atmega2560

Program:    1326 bytes (0.5% Full)
(.text + .data + .bootloader)

Data:          9 bytes (0.1% Full)
(.data + .bss + .noinit)


/usr/local/arduino/hardware/tools/avrdude -C/usr/local/arduino/hardware/tools/avrdude.conf -patmega2560 -cwiring -P/dev/ttyACM0 -b115200 -D -F -V -Uflash:w:dist/Release/Arduino-Linux-x86/blink.hex:i

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9801
avrdude: reading input file "dist/Release/Arduino-Linux-x86/blink.hex"
avrdude: writing flash (0 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 0 bytes of flash written

avrdude done.  Thank you.


BUILD SUCCESSFUL (total time: 480ms)

For those who find this post later:

Reference: http://stackoverflow.com/questions/20356871/avr-gcc-destructive-optimizations

The problem is you cannot use a 'RELEASE' configuration because of the optimization. The '-O2' is the real problem. Use the 'DEBUG' configuration because it doesn't optimize.

Here is the project Makefile sections:
Remember, a tab must precede the commands, not spaces.

.build-post: .build-impl
# Add your post 'build' code here...
 /usr/local/arduino/hardware/tools/avr/bin/avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 ${CND_ARTIFACT_PATH_${CONF}} ${CND_ARTIFACT_PATH_${CONF}}.eep 
 /usr/local/arduino/hardware/tools/avr/bin/avr-objcopy -O ihex -R .eeprom ${CND_ARTIFACT_PATH_${CONF}} ${CND_ARTIFACT_PATH_${CONF}}.hex 


# upload
upload: .build-post
 /usr/local/arduino/hardware/tools/avrdude -C/usr/local/arduino/hardware/tools/avrdude.conf -patmega2560 -cwiring -P/dev/ttyACM0 -b115200 -D -Uflash:w:${CND_ARTIFACT_PATH_${CONF}}.hex:i

NOTE: You may have to change the ttyACM0 to the correct device.

In Netbeans, here are the project preferences I used:

Build->XX Compiiler->Include Directories

/usr/local/arduino/hardware/tools/avr/lib/gcc/avr/4.3.2/include
/usr/local/arduino/hardware/tools/avr/lib/gcc/avr/4.3.2/include-fixed
/usr/local/arduino/hardware/tools/avr/lib/avr/include
/usr/local/arduino/hardware/arduino/cores/arduino
/usr/local/arduino/hardware/arduino/variants/mega

Build->C Compiler->Additional Options

-Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega2560 -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106

Build->C++ Compiler->Additional Options

-Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega2560 -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106

Build->Linker->Additional Options

-Os -Wl,--gc-sections,--relax -mmcu=atmega2560

Seems like a lot of trouble to avoid using the Arduino IDE - but each to his own.

…R