Why does LDFLAGS put my libraries before object files generating undefined references?

573 Views Asked by At

Here's what happens.

$ LDFLAGS=-ltestu01 make exemplo
cc   -ltestu01  exemplo.c   -o exemplo
/home/melba/tmp/ccO2KkjG.o: In function `main':
exemplo.c:(.text+0x6e): undefined reference to `unif01_CreateExternGenBits'
exemplo.c:(.text+0x7e): undefined reference to `bbattery_SmallCrush'
exemplo.c:(.text+0x8a): undefined reference to `unif01_DeleteExternGenBits'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'exemplo' failed
make: *** [exemplo] Error 1
%

I expected the command to be cc exemplo.c -o exemplo -ltestu01. How can I make sure the hints to the linker go to the end of the command line?

1

There are 1 best solutions below

0
On BEST ANSWER

make -p prints the default recipes.

Your recipe should be:

%: %.c
#  recipe to execute (built-in):
        $(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@

From this, it should be obvious you should be setting LDLIBS, not LDFLAGS.

make exemplo LDLIBS=-ltestu01 -B

runs

cc     examplo.c  -ltestu01 -o exemplo

as expected. It seems the intention is that LDFLAGS is for things like -Wl,--something.