Undefined link, maybe makefile is wrong?

I had some problems earlier with declaring an array of arrays. Now I think that something is 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 in my set.c file, but these are the functions that I am currently testing:

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

And the errors that I get in the terminal:

 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' 
+4
source share
3 answers

The errors you get are linker errors telling you that when linking your program, the linker cannot find a function called "CreateSet" (etc.). It is not immediately clear why this should be so, because it seems that you include the set.o command in the build command.

To fix build problems, it’s often useful to find out what the attempt does and then run the commands individually, one at a time, so you can see where everything goes wrong. "make -n" will show you which "make" commands will be executed without executing them. I would expect to see a command like:

 gcc -o proj1 main.o set.o 

try running it manually and see where it takes you.

+5
source

If they are all on the same line in the makefile:

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

Then you have one EEXEC macro whose value is proj1 CC = gcc CFLAGS = -c -Wall , and you do not have the CC or CFLAGS macro. CC is probably the default, so a lot works.

0
source

Make sure you include set.h in main.c
You also declare EEXEC, but use EXEC ...

0
source

All Articles