0

I have the following headers and source files:

AVR_comunic.h

#ifndef AVR_COMUNIC_H_
#define AVR_COMUNIC_H_

#include<avr/io.h>

#define FOSC 20000000UL // Clock Speed
#define BAUD 19200
#define MYUBRR FOSC/16/BAUD-1

void USART_Init( unsigned int);
int USART_WriteChar(unsigned char);
// int USART_Receive( unsigned char *);
void USART_Flush(void);

#endif /* AVR_COMUNIC_H_ */

AVR_comunic.c

#include "AVR_comunic.h"

void USART_Flush( void )
{
    ...
}

void USART_Init( unsigned int ubrr)
{
    ...
}

int USART_WriteChar(unsigned char data)
{
   ...
}

and main_f.c

#include"AVR_comunic.h"

void init_ports()
{
    DDRD|= (1<<PD7);
}

int main(void)
{
    init_ports();
    ...
    while(1)
    {
        // now what?
    }
return 1;
}

I wrote a batch file in Notepad++ to obtain the .hex file for this program

COMPILE_BUILD.bat

ECHO OFF
ECHO ----------------------------------------------------------
ECHO ----- executing your commands : compile and build  -------
ECHO ----------------------------------------------------------
avr-gcc -g -Wall -Os -DF_CPU=20000000UL -mmcu=atmega88p -c AVR_comunic.c 
avr-gcc -g -Wall -Os -DF_CPU=20000000UL -mmcu=atmega88p -c main_f.c

avr-gcc -g -mmcu=atmega88p -o AVR_comunic.elf main_f.o AVR_comunic.o

avr-objdump -h -S AVR_comunic.elf > AVR_comunic.lst
avr-gcc -g -mmcu=atmega8 -Wl,-Map,AVR_comunic.map -o AVR_comunic.elf AVR_comunic.o
avr-objcopy -j .text -j .data -O ihex AVR_comunic.elf AVR_comunic.hex
ECHO ----------------------------------------------------------
ECHO ---------------------- happy ? ---------------------------
ECHO ----------------------------------------------------------
pause

I get the following error:

... : undefined reference to 'main'
avr-objcopy: 'AVR_comunic.elf' no such file

I understand from the information on internet that the linker does thinks that there is no "main" function ... but it is ... what am I doing wrong? Or how should I write to compile multiple source files?

3
  • avr-objdump needs to be after avr-gcc ... -o AVR_comunic.elf as that's what creates AVR_comunic.elf. Second why aren't you using main_f.elf as that's where main should be part of the the object. I don't see how AVR_comunic.elf contains main either, given it's only object file AVR_comunic.o. Commented Aug 22, 2017 at 23:42
  • I would advise to use the IDE with automatic makefile generation. If you add libraries, C files they will be automatically compiled and linked. Batch files are not used for buiding targets. If you for some strange reasons do not want to use Atmel studio or eclipse with avr plugins - learn make Commented Aug 23, 2017 at 0:31
  • @PeterJ_01 I use eclipse IDE with avr plugins, but somebody mentioned here about some linker script (I've never done anything like that) and I thought that maybe I should get a little more comfortable with compiling and linking ... Commented Aug 23, 2017 at 6:50

1 Answer 1

1

I understand from the information on internet that the linker does thinks that there is no "main" function ... but it is ... what am I doing wrong?

This command:

avr-objdump -h -S AVR_comunic.elf > AVR_comunic.lst

attempts to extract info from AVR_communic.elf, which isn't produced yet. You want to move it after this command:

avr-gcc -g -mmcu=atmega8 -Wl,-Map,AVR_comunic.map -o AVR_comunic.elf AVR_comunic.o

The above command fails with undefined reference to: 'main' because you are not linking main_f.o into it.

It appears that what you really want is:

avr-gcc -g -mmcu=atmega88p -o AVR_communic.elf main_f.o AVR_comunic.o
avr-objdump -h -S AVR_comunic.elf > AVR_comunic.lst
avr-objcopy -j .text -j .data -O ihex AVR_comunic.elf AVR_comunic.hex
Sign up to request clarification or add additional context in comments.

5 Comments

Sir, thank you very much for your answer. I've modified my code but as you can see, I get the same (similar) error.
@CMarius That's because you can't follow simple instructions. In particular, you left the broken command in instead of deleting it. You have 4 invocations of avr-gcc, and the last one is bogus and should be deleted.
Or instead of deleting it (I've learned that that file contains some info about memory ... ) I should write: avr-gcc -g -mmcu=atmega8 -Wl,-Map,AVR_comunic.map -o AVR_comunic.elf AVR_comunic.o main_f.o IS this correct (I does not give any error)?
@CMarius Sorry I missed the -Wl,-Map,AVR_communic.map argument. You can add it to the 3rd invocation of avr-gcc (i.e. to the link line), and delete the 4th invocation.
Sorry for keeping commenting ... but if the invocation remains as written now (it does compile now that I've added main_f.o) is everything OK? I do not know much yet about this map file ... but before I learn, at least to know I have it correctly :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.