Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference maybe makefile is wrong?

Tags:

c

makefile

I had some issues earlier with declaring my array set of records. Now I think there is something wrong with my Makefile or something.

Here is my Makefile:

EEXEC = proj1  
CC = gcc  
CFLAGS = -c -Wall  

$(EXEC) :   main.o set.o 
    $(CC) -o $(EXEC) main.o set.o 

main.o  :   main.h main.c
    $(CC) $(CFLAGS) main.c  

set.o   :   set.h set.c
    $(CC) $(CFLAGS) set.c   

There are more functions I have in my set.c file but these are the functions I am testing at the moment:

DisjointSet *CreateSet(int numElements);  
DisjointSet *MakeSet(DisjointSet *S,int ele, int r);  
void Print(DisjointSet *S);

And the errors I am receiving in the terminal is:

main.o: In function `main':  
main.c:(.text+0x19): undefined reference to `CreateSet'  
main.c:(.text+0x43): undefined reference to `MakeSet'  
main.c:(.text+0x5f): undefined reference to `Print'  
like image 488
Jeremy Avatar asked Nov 09 '10 05:11

Jeremy


1 Answers

The errors that you're getting are linker errors, telling you that while linking your program the linker can't find a function named 'CreateSet' (etc.). It's not immediately obvious why that should be the case, because it appears that you're including "set.o" in the build command.

To troubleshoot build problems, it's often useful to figure out what make is trying to do, and then run the commands individually one at a time so you can see where things go wrong. "make -n" will show you what commands "make" would run, without actually doing them. I would expect to see a command like:

gcc -o proj1 main.o set.o

try running that by hand and see where it gets you.

like image 67
David Gelhar Avatar answered Oct 31 '22 18:10

David Gelhar