Timing problems with programs compiled using Eclipse

Hello there,
First things first, I'm using Linux-x86_64.
I'm starting a rather complex project that will probably require a lot more source tracking (ie. GIT) than the Arduino IDE can manage.
So, i followed the instructions on the playground to get Eclipse, the arduino lib and the toolchain set up to develop there.
It DOES compile things, and AVRDUDE Does upload, when the programmer is set to STK500V2.
The strange problem is that i can't get the timing right on my Arduino. I'm using a Mega2560, so, naturally, the AVR target properties for the library
and the code projects is a Mega2560, running at 16Mhz (setting: 16000000).

Now, i have compiled the basic library as libArduino.a, and i have made a little test program:

#include <Arduino.h>
void setup() {
	pinMode(13,OUTPUT);
}

void loop() {
	digitalWrite(13,HIGH);
	delay(1000);
	digitalWrite(13,LOW);
	delay(1000);
}

The timing IS NOT RIGHT. The led does not blink then wait one second before turning off, and one second before turning on again. Instead, it just blinks madly, twice, then delays randomly before looping all over again. (Much less than a second)
The compiler output is, if it is any help:

**** Build of configuration 2560 for project hemsutClient ****

make all 
Building file: ../src/client.cpp
Invoking: AVR C++ Compiler
avr-g++ -I"/home/rasheek/workspace/Arduino/src" -I"/home/rasheek/workspace/hemsutClient/arduinolib" -I"/home/rasheek/workspace/hemsutClient/lib" -DARDUINO=100 -Wall -Os -ffunction-sections -fdata-sections -fno-exceptions -mmcu=atmega2560 -DF_CPU=16000000UL -MMD -MP -MF"src/client.d" -MT"src/client.d" -c -o "src/client.o" "../src/client.cpp"
Finished building: ../src/client.cpp
 
Building target: hemsutClient.elf
Invoking: AVR C++ Linker
avr-g++ --cref -s -Os -o"hemsutClient.elf"  ./src/client.o  ./arduinolib/Dhcp.o ./arduinolib/Dns.o ./arduinolib/EEPROM.o ./arduinolib/Ethernet.o ./arduinolib/EthernetClient.o ./arduinolib/EthernetServer.o ./arduinolib/EthernetUdp.o ./arduinolib/SPI.o ./arduinolib/socket.o ./arduinolib/w5100.o   -lArduino -lm -lm  -Wl,-Map,hemsutClient.map,--cref -Wl,--gc-sections -L"/home/rasheek/workspace/Arduino/2560_16M" -mmcu=atmega2560
Finished building target: hemsutClient.elf
 
Invoking: Print Size
avr-size --format=avr --mcu=atmega2560 hemsutClient.elf
AVR Memory Usage
----------------
Device: atmega2560

Program:    2976 bytes (1.1% Full)
(.text + .data + .bootloader)

Data:         69 bytes (0.8% Full)
(.data + .bss + .noinit)


Finished building: sizedummy
 

**** Build Finished ****

I'm at a loss. I'm not sure what is going on here. I hope someone can give me some pointers!
-R

First thing I'd try would be compiling and linking using the IDE, with verbose mode enabled. Compare the resulting behavior on the Arduino and the output.

I went through the build process with a fine tooth-comb. Yet, i can't find the flaw in the process.
One thing i have noticed is that Eclipse is not using stripped-down .hex files, instead, it is just loading the raw AVR binary to the MCU.
I really don't know where to look next. I have found no real difference (to my reading) of the build process. Maybe it's the Hex files instead of the elf that ought to be loaded?

Here's the relevant build outputs of EQUAL projects: (as attachments)

The only real differences i can spot are in the hex files, but, i really don't know what difference this might, or might not make.
Another observation is that after i compile the code with eclipse, the MCU takes two or three seconds before running it, whereas Arduino's equivalent starts up as soon as power is applied.

Thanks in advance for any insight.
-r

build-arduino-blink (9.53 KB)

build-libarduino (10.8 KB)

build-project (11.6 KB)

rasheek:
it just blinks madly, twice, then delays randomly before looping all over again. (Much less than a second)

That doesn't sound like a timing problem - more like your Arduino is not executing your code (perhaps it's executing a corrupted version due to an incorrect build/upload process). (Sorry, I have no idea what might be incorrect about it.)

Okay, this was a headache, but i did go deep into the less-than-transparent arduino process.
It turns out several things need to happen for the program to run correctly:

  1. Make SURE the main() function is declared:
int main(void)
{
   init();

   setup();

   for (;;)
      loop();

   return 0;
}

2 - make sure the compiler is compiling with the -g -Os flags
3 - Finally, DO not flash ELF files to your Atmega! You must strip the elf to make a HEX file for the arduino bootloader to run, as such (my program is called hemsutClient):

avr-objcopy -O ihex -R .eeprom hemsutClient.elf hemsutClient.hex

The easy way to do this in eclipse is to set a post-build command as such:

 avr-objcopy -O ihex -R .eeprom ${workspace_loc}/${ProjName}/2560/hemsutClient.elf ${workspace_loc}/${ProjName}/2560/hemsutClient.hex

Hopefully this will be helpful to someone else!

-R