views:

758

answers:

2

Hi Guys, I'm working on a C project with around 30 source files (.c). I'm building this project on a 32 bit micro-controller(i.MX515) running on Ubuntu using GNU tools.

The compilation phase completes successfully, however when the linking process starts I get this error (For full error at the end of the quesiton):

In function `_start': init.c:(.text+0x30): undefined reference to `main'

I have a main() function which does a simple printf().

My Makefile line for linking, looks like this.

final: $(OBJDIR)/main.o $(OBJDIR)/TchClaKnn_BuildKdtreeInt.o $(OBJDIR)/TchClaKnn_FreeKdtreeInt.o.... (Go upto 30 files like this)
    @echo ".Linking"
    $(CC) $(LFLAGS) -o $(OBJDIR)/main.o $(OBJDIR)/TchClaKnn_BuildKdtreeInt.o $(OBJDIR)/TchClaKnn_FreeKdtreeInt.o..... (Go upto 30 files like this)

Help!!!

Regards

Vikram


Complete linking error

/usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o: In function `_start':
init.c:(.text+0x30): undefined reference to `main'
collect2: ld returned 1 exit status
make[1]: *** [final] Error 1
make[1]: Leaving directory `/home/ubuntu/Documents/Project/IMX_Project_v1'
make: *** [all] Error 2
+1  A: 

The linker cannot find the object code for the source that has main() defined in it. Probably its not getting compiled. Check that whichever of the 30 files it is that has main() in it, is getting compiled to .OBJ.

RdM
Hi RM, yes you were right. The object file for my main.c was not getting generated, because I had a mistake in my makefile I fixed it and now my final is getting generated. Thank you!!!
vikramtheone
+3  A: 

final depends on main.o (and a bunch of others), but, your makefile is taking all the 'others' and outputting them in main.o (that's what -o does in most compilers)

edit your link line to : -o final $(OBJDIR)/main.o

KevinDTimm
Hi Kevin, YES!!! My makefile was erroneous, I introduced what you suggested and it worked. I do this mistake quite often, when I went through one of my very old makefile I see that I had - "$(CC) $(LFLAGS) -o $@" in the linking command line. Well, in both the cases the final gets generated. Thank you!!!
vikramtheone
You're welcome, it's why another set of eyes almost always finds the problem right away - they don't know what to 'skip over' ;)
KevinDTimm