"/usr/bin/ld: Undefined Symbol" on Mac OS
Just ran across a really annoying quirk in the Mac OS gcc linker. The following makefile failed with numerous "undefined symbols" even though I was linking to the correct libraries (or so I thought):
CFLAGS=-g -Wall -I${PVM_ROOT}/include/
LDFLAGS=-L${PVM_ROOT}/lib/${PVM_ARCH}/ -lpvm3
all: clean problem1_pvm MatrixTest
clean:
rm -f *.o
rm -f problem1_pvm
problem1_pvm: problem1_pvm.o Matrix.o Exception.o
$(CC) $(LDFLAGS) -o $@ problem1_pvm.o Matrix.o Exception.o
MatrixTest: MatrixTest.o Matrix.o Exception.o
$(CC) $(LDFLAGS) -o $@ MatrixTest.o Matrix.o Exception.o
problem1_pvm.o: problem1_pvm.c problem1_pvm.h
$(CC) $(CFLAGS) -o $@ -c problem1_pvm.c
MatrixTest.o: MatrixTest.c
$(CC) $(CFLAGS) -o $@ -c MatrixTest.c
Matrix.o: Matrix.c Matrix.h
$(CC) $(CFLAGS) -o $@ -c Matrix.c
Exception.o: Exception.c Exception.h
$(CC) $(CFLAGS) -o $@ -c Exception.c
The problem was that I had $(LDFLAGS) before the object files in command line. It has something to do with the Mac OS gcc being a single-pass linker or something. Anyway, moving $(LDFLAGS) to the end of the line solved the problem:
CFLAGS=-g -Wall -I${PVM_ROOT}/include/
LDFLAGS=-L${PVM_ROOT}/lib/${PVM_ARCH}/ -lpvm3
all: clean problem1_pvm MatrixTest
clean:
rm -f *.o
rm -f problem1_pvm
problem1_pvm: problem1_pvm.o Matrix.o Exception.o
$(CC) -o $@ problem1_pvm.o Matrix.o Exception.o $(LDFLAGS)
MatrixTest: MatrixTest.o Matrix.o Exception.o
$(CC) -o $@ MatrixTest.o Matrix.o Exception.o $(LDFLAGS)
problem1_pvm.o: problem1_pvm.c problem1_pvm.h
$(CC) $(CFLAGS) -o $@ -c problem1_pvm.c
MatrixTest.o: MatrixTest.c
$(CC) $(CFLAGS) -o $@ -c MatrixTest.c
Matrix.o: Matrix.c Matrix.h
$(CC) $(CFLAGS) -o $@ -c Matrix.c
Exception.o: Exception.c Exception.h
$(CC) $(CFLAGS) -o $@ -c Exception.c
