Undefined reference maybe makefile is wrong?
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'
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.
Make sure you have included set.h in main.c
Also you declare EEXEC but use EXEC...
If these are all on one line in the makefile:
EEXEC = proj1 CC = gcc CFLAGS = -c -Wall
Then you have one macro EEXEC
whose value is proj1 CC = gcc CFLAGS = -c -Wall
, and you have no CC
or CFLAGS
macro. CC
probably has a default, which is why that much is working.
精彩评论