Programming Arduino from terminal

Hi,
I'm experimenting with an Arduino Uno R3 and a MacBook. I want to compile and load a code via terminal, so, I go in the Arduino folder and I search for avr-gcc and avrdude.
I compile using avr-gcc a very simple Blink program that turns on and off a LED, and I get the hex file with no errors or warnings.
Then I run in the terminal (from the Arduino.app/Contents/Resources/Java/hardware/tools/avr/ folder)

./bin/avrdude -C ./etc/avrdude.conf -p m328p -c arduino -P /dev/tty.usbmodem641 samplecode.hex

To push the hex file into the Arduino, and I get the following output

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f

avrdude: safemode: Fuses OK

avrdude done.  Thank you.

It seems that everything is ok, but the led doesn't blink, so the code has not been loaded on the Arduino. I double checked the code to be sure that's not a code problem. The avrdude configuration file is from the arduino IDE, so it should work. I think the problem is the programmer, but I don't know, maybe the problem is somewhere else.
Oh, and, if you know, is there a way to use Arduino as ISP via terminal?

Thank you in advance

It seems that everything is ok, but the led doesn't blink, so the code has not been loaded on the Arduino.

I can't see how you jump all the way to the conclusion that avrdude is faulty. Your (un posted code however ....)

Mark

Here is the test code, it is very simple, I don't think the problem it this

#define F_CPU 16000000
#define __AVR_ATmega328P__

#include <avr/io.h>
#include <util/delay.h>

void setup() {
    DDRB |= _BV(PINB5); // set pin 13 as output
}

void loop() {
    PORTB |= _BV(PINB5);
    _delay_ms(500);
    PORTB &= ~_BV(PINB5);
    _delay_ms(500);
}

compiled with:

avr-gcc -O2 -o samplecode.hex samplecode.c

Where is main.c how did you link it? looks like you may have an empty .hex

Mark

No, the hex file is 2.0Kb, it is not empty.
I was using an avr-gcc downloaded from the internet that maybe adds automatically the main().
However I deleted it, and using the avr-gcc inside the arduino I had to rewrite the code:

#define F_CPU 16000000
#define __AVR_ATmega328P__

#include <avr/io.h>
#include <util/delay.h>

void loop(void);
void setup(void);

int main() {
    setup();
    while(1)
        loop();
}

void setup(void) {
    DDRB |= _BV(PINB5); // set pin 13 as output
}

void loop(void) {
    PORTB |= _BV(PINB5);
    _delay_ms(500);
    PORTB &= ~_BV(PINB5);
    _delay_ms(500);
}

However reading the avrdude manual I discovered that the avrdude command I typed is wrong, because it doesn't write nothing on the Arduino. The right command is this:

avrdude -C [optional, path of avrdude.conf] -p m328p -c arduino -P /dev/tty.usbmodem641 -e -U flash:w:"./samplecode.hex"
-U flash:w:"./samplecode.hex"

hah! Yes; you figured it out before I got around to answering!
So is everything working now?

Now running the same avrdude command I get this error:

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude: erasing chip
avrdude: reading input file "./test.hex"
avrdude: input file ./test.hex auto detected as invalid format
avrdude: invalid input file format: -1
avrdude: read from file './test.hex' failed

avrdude: safemode: Fuses OK

avrdude done.  Thank you.

but if I compile with --mmcu=atmega328p I don't get that error, but my code seems to not work. It should switch on a led connected on pin 4. Here is my test code

#ifndef __AVR_ATmega328P__ 
    #define __AVR_ATmega328P__  // microcontroller
#endif
#define F_CPU 16000000      // frequency of the cpu

#include <avr/io.h>
//#include <avr/interrupt.h>
#include <util/delay.h>
#include <util/atomic.h>

int main() {
    DDRD = _BV(PIND4); // pin 4
    PORTD = _BV(PIND4);

    while (1) {
        _delay_ms(1000);
        PORTD = _BV(PIND4);
    }
    
    return 0;
}
avr-gcc -O2 -o samplecode.hex samplecode.c

avr-gcc does not produce .hex files, it produces elf format files which you would then convert to .hex files with "avr-objcopy" Something like:

 avr-objcopy -O ihex -R .eeprom a.out float.hex

Being a "well behaved" unix-style program, avr-gcc will happily and silently allow you to name the elf file with a .hex "type", even though the format is entirely different.