UPDATE: Found the problem. It was a bug on my end and had nothing to do with make. I had edited the file to remove the preprocessor conditional and botched it.
I am attempting to compile a testing program using a makefile to handle the creation of a library of files that include functions which are used by the test. When trying to make the final executable, I receive an 'undefined reference' error. Below is my makefile and the resulting output. I have tried switching up the ordering of files and operations within my makefile, but they all resulted in failure. That leads me to believe I am missing some key component to making it work instead of needing to reorder my operations.
I am running RHEL 4 if that matters.
CC=gcc
CFLAGS=-Wall
LDFLAGS=-lm
AR=ar
OBJ = \
test.o \
disk_alloc.o \
stringtools.o \
path.o \
xxmalloc.o
LIB = libtest.a
PROG = test
TAR = $(LIB) $(PROG)
all: $(TAR)
libtest.a: $(OBJ)
$(AR) -rv $(LIB) $(OBJ)
ranlib $(LIB)
$(PROG):
$(CC) $(CFLAGS) $(LIB) -o $@
clean:
rm -f $(OBJ) $(TAR)
.PHONY: all clean
# vim: set noexpandtab tabstop=4:
Output after executing make:
gcc -Wall -c -o test.o test.c
gcc -Wall -c -o disk_alloc.o disk_alloc.c
gcc -Wall -c -o stringtools.o stringtools.c
gcc -Wall -c -o path.o path.c
gcc -Wall -c -o xxmalloc.o xxmalloc.c
ar -rv libtest.a test.o disk_alloc.o stringtools.o path.o xxmalloc.o
ar: creating libtest.a
a - test.o
a - disk_alloc.o
a - stringtools.o
a - path.o
a - xxmalloc.o
ranlib libtest.a
gcc -Wall libtest.a -o test
libtest.a(test.o): In function `disk_alloc_test_empty':
test.c:(.text+0x5f): undefined reference to `disk_alloc_create'
test.c:(.text+0x81): undefined reference to `disk_alloc_delete'
libtest.a(test.o): In function `disk_alloc_test_read_write':
test.c:(.text+0x17b): undefined reference to `disk_alloc_create'
test.c:(.text+0x1e0): undefined reference to `disk_alloc_delete'
libtest.a(test.o): In function `disk_alloc_test_nested':
test.c:(.text+0x30a): undefined reference to `disk_alloc_create'
test.c:(.text+0x372): undefined reference to `disk_alloc_create'
test.c:(.text+0x410): undefined reference to `disk_alloc_delete'
test.c:(.text+0x426): undefined reference to `disk_alloc_delete'
collect2: ld returned 1 exit status
make: *** [test] Error 1