Always undefined reference to `main'

Go To Last Post
21 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Every example code ends to the undefined reference to `main' error.

Build started 4.12.2007 at 17:58:47
avr-gcc.exe -mmcu=at90s2313 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT demo.o -MF dep/demo.o.d -c ../demo.c
avr-gcc.exe -mmcu=at90s2313 -Wl,-Map=demo.map demo.o -o demo.elf
c:/program files/winavr/bin/../lib/gcc/avr/4.1.2/../../../../avr/lib/crts2313.o: In function `__vectors':
../../../../../avr-libc-1.4.6/crt1/gcrt1.S:51: undefined reference to `main'
make: *** [demo.elf] Error 1
Build failed with 1 errors and 0 warnings...

Even with code like this:

#include 

// Define the bits in the port 
typedef struct 
{ 
   unsigned char bit0 : 1, 
                 bit1 : 1, 
                 bit2 : 1, 
                 bit3 : 1, 
                 bit4 : 1, 
                 bit5 : 1, 
                 bit6 : 1, 
                 bit7 : 1; 
} bit_field; 

// Define macro to get the value of each bit 
#define GET_BIT(port) (*(volatile bit_field *) (_SFR_ADDR(port))) 

// Define functions for each bit of the I/O ports in the program 
#define SIG    GET_BIT(PINB).bit0
#define LED	   GET_BIT(PORTD).bit0

int main (void)
{
	for (;;)
	{
		if (SIG) LED = 1;
			else LED = 0;
	}
}
 

I mean EVERY example code compiling! I am a total newbie with AVR, remember that when replying :)

I'm a Newbie! ... and it's not my fault!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just lifted that code, pasted it into an empty project in Studio and clicked the build button:

AVR Memory Usage
----------------
Device: at90s2313

Program:      90 bytes (4.4% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)


Build succeeded with 0 Warnings...

So it definitely will build OK and this rules out the C code itself. So it's something about your build environment - maybe your Makefile?

(BTW you aren't making the classic error (that I'm sure we've all done at one time) of edting the code in one place but actually building something else that you aren't changing?)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
So it's something about your build environment - maybe your Makefile?

Yes it was makefile. Because I didn't even knew what was it :lol: Now I unerstand what is makefile and no errors anymore. Thanks.

I'm a Newbie! ... and it's not my fault!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry to drag up an old thread... But can anyone tell me how to solve this?

I'm getting the same error, but my makefile appears ok?

Build started 12.3.2008 at 12:45:34
avr-gcc.exe -mmcu=atmega8  Nixie_clock.o lcd.o   -L"C:\Documents and Settings\Dave\Desktop\AVR Code\m8 Projects\Nixie_clock\."   -o Nixie_clock.elf
c:/winavr-20071221rc1/bin/../lib/gcc/avr/4.2.2/../../../../avr/lib/avr4/crtm8.o: In function `__vectors':
../../../../crt1/gcrt1.S:52: undefined reference to `main'
make: *** [Nixie_clock.elf] Error 1
Build failed with 1 errors and 0 warnings...

Dave Harrod

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well do you have a file being linked that is either from a .S asm file with:

main:

or from a .c file with:

int main(void) {

In other words does either Nixie_clock.S/.c or lcd.S/.c contain a "main" function which is where the C compiler expects to enter your own code.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just found this thread. I too am having this problem. The error is as follows.

Build started 10.5.2008 at 09:34:03
avr-gcc.exe -mmcu=atmega8 -Wl,-Map=final_final.map Final_final.o    -lc -lm  -o final_final.elf
c:/winavr-20080430/bin/../lib/gcc/avr/4.3.0/../../../../avr/lib/avr4/crtm8.o:(.init9+0x0): undefined reference to `main'
make: *** [final_final.elf] Error 1
Build failed with 1 errors and 0 warnings...

Cliff, in reference to your last post

Quote:

In other words does either Nixie_clock.S/.c or lcd.S/.c contain a "main" function which is where the C compiler expects to enter your own code.

and based on my program, no, there are no other "main" functions identified anywhere. Any one have any suggestions?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Like I said in the other thread (this is just on the verge of a "cross post" by the way!) you may well be editing the "int main(void)" in one file but that file probably isn't getting included in the build for some reason.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If it's possible (I don't know how) I can delete the other post. I found this one after I had made a new thread.

I only have one source file. All functions definitions are included in that file.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just a minute - have you now deleted your code. I just added this text to the "other thread" but then thought I'd picked the wrong one. Anyway, this IS the problem...

Blimey it took a while to spot it (I just tried #if 0'ing out bits of the code until I found the problem) but one clue was those additional } at the end of the file beyond main() - they had to be there for the file to compile which meant there were some unmatched braces somewhere and therefore main() was being defined INSIDE another function. Another clue to it was the compiler warning:

../../../../../test/var.c:289: warning: 'main' is normally a non-static function

The fault is actually:

   while (k

I bet the second brace of that inner if() is supposed to be a CLOSE brase, not an OPEN brace!

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

one clue was those additional } at the end of the file beyond main() - they had to be there for the file to compile which meant there were some unmatched braces somewhere

We'll it will teach the OP not to merely add closing braces where the compier says it is missing one, but to actually go look for the reason... Good catch though, Cliff!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you! I don't know that I ever would have found that. Now if I can just get it to work properly.... :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Once again this proves that build time warnings should not be ignored - many are often benign but some are actually giving you a clue to something really serious (like the warning you get when you make the classic "if (a=2) {" error)

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Play tricks again! Where problem?
Code:

#include "spi.h"

#include
#include

#define PORT_SPI PORTB
#define DDR_SPI DDRB
#define DD_MISO DDB4
#define DD_MOSI DDB3
#define DD_SS DDB2
#define DD_SCK DDB5

void spi_init()
// Initialize pins for spi communication
{
DDR_SPI &= ~((1<<DD_MOSI)|(1<<DD_MISO)|(1<<DD_SS)|(1<<DD_SCK));
// Define the following pins as output
DDR_SPI |= ((1<<DD_MOSI)|(1<<DD_SS)|(1<<DD_SCK));

SPCR = ((1<<SPE)| // SPI Enable
(0<<SPIE)| // SPI Interupt Enable
(0<<DORD)| // Data Order (0:MSB first / 1:LSB first)
(1<<MSTR)| // Master/Slave select
(0<<SPR1)|(1<<SPR0)| // SPI Clock Rate
(0<<CPOL)| // Clock Polarity (0:SCK low / 1:SCK hi when idle)
(0<<CPHA)); // Clock Phase (0:leading / 1:trailing edge sampling)

SPSR = (1<<SPI2X); // Double Clock Rate

}
/*
void spi_transfer_sync (uint8_t * dataout, uint8_t * datain, uint8_t len)
// Shift full array through target device
{
uint8_t i;
for (i = 0; i < len; i++) {
SPDR = dataout[i];
while((SPSR & (1<<SPIF))==0);
datain[i] = SPDR;
}

*/

void spi_transmit_sync (uint8_t * dataout, uint8_t len)
// Shift full array to target device without receiving any byte
{
uint8_t i;
for (i = 0; i < len; i++) {
SPDR = dataout[i];
while((SPSR & (1<<SPIF))==0);
}
/*

uint8_t spi_fast_shift (uint8_t data)
// Clocks only one byte to target device and returns the received one
{
SPDR = data;
while((SPSR & (1<<SPIF))==0);
return SPDR;
}
*/
}
Message:
rm -rf spi.o spi.elf dep/* spi.hex spi.eep spi.lss spi.map
Build succeeded with 0 Warnings...
avr-gcc.exe -mmcu=atmega8 -Wall -gdwarf-2 -std=gnu99 -DF_CPU=4000000UL -O2 -funsigned-char -MD -MP -MT spi.o -MF dep/spi.o.d -c ../spi.c
avr-gcc.exe -mmcu=atmega8 -Wl,-Map=spi.map spi.o -o spi.elf
d:/winavr/bin/../lib/gcc/avr/4.3.0/../../../../avr/lib/avr4/crtm8.o:(.init9+0x0): undefined reference to `main'
make: *** [spi.elf] Error 1
Build failed with 1 errors and 0 warnings...

Makefile:

###############################################################################
# Makefile for the project spi
###############################################################################

## General Flags
PROJECT = spi
MCU = atmega8
TARGET = spi.elf
CC = avr-gcc.exe

## Options common to compile, link and assembly rules
COMMON = -mmcu=$(MCU)

## Compile options common for all C compilation units.
CFLAGS = $(COMMON)
CFLAGS += -Wall -gdwarf-2 -std=gnu99 -DF_CPU=4000000UL -O2 -funsigned-char
CFLAGS += -MD -MP -MT $(*F).o -MF dep/$(@F).d

## Assembly specific flags
ASMFLAGS = $(COMMON)
ASMFLAGS += $(CFLAGS)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2

## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS += -Wl,-Map=spi.map

## Intel Hex file production flags
HEX_FLASH_FLAGS = -R .eeprom

HEX_EEPROM_FLAGS = -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings

## Objects that must be built in order to link
OBJECTS = spi.o

## Objects explicitly added by the user
LINKONLYOBJECTS =

## Build
all: $(TARGET) spi.hex spi.eep spi.lss size

## Compile
spi.o: ../spi.c
$(CC) $(INCLUDES) $(CFLAGS) -c $<

##Link
$(TARGET): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET)

%.hex: $(TARGET)
avr-objcopy -O ihex $(HEX_FLASH_FLAGS) $< $@

%.eep: $(TARGET)
-avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0

%.lss: $(TARGET)
avr-objdump -h -S $< > $@

size: ${TARGET}
@echo
@avr-size -C --mcu=${MCU} ${TARGET}

## Clean target
.PHONY: clean
clean:
-rm -rf $(OBJECTS) spi.elf dep/* spi.hex spi.eep spi.lss spi.map

## Other dependencies
-include $(shell mkdir dep 2>/dev/null) $(wildcard dep/*)

Where is problem?
Many other projects fooling too.
Please not beat me. Iam very new in Winavr,

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You'd stand a better chance of getting help if you presented the code in a somewhat more readable form. Learn how to use the forum code-tags to keep the formatting of source code.

Quote:

Where problem?

The problem is exactly what the error message says. Therre is no main() in your source file.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank You Johan!

But in code problems.

Whats are from code
(uint8_t * dataout, uint8_t * datain, uint8_t len)
len?
datain? Inputs ? and how declare
dataout may be OK ... made output byte?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What you are looking at there is just some "support code" (presumably in a file called spi.c). It's just providing the routines to do SPI for you but you have to have a program that actually makes use of the functions it provides and the entry point to all C programs is always to a function called main()

So either in the top of that file or in a separate file that you have built/linked with this one put something like

int main(void) {
 uint8_t data_to_send[4] = { 0x13, 0xBC, 0xF2, 0x3A };
 uint8_t data_received[4];

 spi_init();
 spi_transfer_sync (data_to_send, data_received, 4);

 while(1);
}

But be warned that this is NOT going to work. I don't know where you found this spi.c but they are blocking on SPIF but never clearing it.

You may find it easier (because SPI really is simple in fact) to forget trying to use someone else's code of unknown provenence and just read the datasheet and then write your own spi_init() and spi_send_receive() routines.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> ...but they are blocking on SPIF but never clearing it.

SPIF is kind of an "SPI shift register busy" flag, as such, it self-clears
provided the SPI unit is actually enabled (so it is clocked).

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dl8dtl wrote:
> ...but they are blocking on SPIF but never clearing it.

SPIF is kind of an "SPI shift register busy" flag, as such, it self-clears
provided the SPI unit is actually enabled (so it is clocked).


Clearly more caffeine is required here - I was thinking of ADIF - doh! :oops:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what changes to made with the makefile ?

mine is

MCU= atmega32

F_CPU=8000000

FORMAT=ihex

OBJDIR=.

SRC=main1.c

SRC=bmp085.c

SRC=twinmaster.c

SRC=uart.c

 

...

had to include bmp085,twinmaster and uart for a purpose 

REST is unchanged

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

+=

 

Really should not have hijacked this thread.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A trap for fresh installs of Eclipse and the AVR plugin over CDT.

*Save* the source if you have just pasted an example in to test.

Build may not be prompting you to save modified code....

Produces exactly the same error as the source is empty.

(signature pending)