tags:

views:

64

answers:

1

I implement a link list in two files in linklist.h and linklist.c, and I call some functions defined in linklist.h in main function of main.c. linklist.h is included in both linklist.c and main.c. When I compile this program by GCC with Makefile, the error named "undefined reference to xxx" occurs. I think my Makefile is written correctly as below. So what is the possible reason for this linking error

CC=gcc

CFLAGS= -g -O2

TARGET=target

OBJECTS=main.o linklist.o

TARGET: $(OBJECTS)

 $(CC) $(CFLAGS) $(OBJECTS) -o $(TARGET)

clean:

 rm target $(OBJECTS)

main.o:linklist.h

linklist.o:linklist.h 
+1  A: 

The makefile looks OK.

Look carefully at the spellings of the function name reported by the linker and at the names in the source code.

Check whether the function is declared static in linklist.c; if it is, it is not available in main.c.

Otherwise, we're likely to need to see your code and the actual error.

Jonathan Leffler
thanks, I check the source code again, the spelling of the function name is a little different in linklist.c and linklist.h, so this is the root cause just as what you have mentioned