为什么 LDFLAGS 将我的库放在生成未定义引用的目标文件之前?

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

事情是这样的。

$ 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
%

我预计命令是 cc exemplo.c -o exemplo -ltestu01。如何确保链接器的提示到达命令行的末尾?

make -p 打印默认配方。

你的食谱应该是:

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

由此可见,您应该设置 LDLIBS,而不是 LDFLAGS

make exemplo LDLIBS=-ltestu01 -B

运行

cc     examplo.c  -ltestu01 -o exemplo

符合预期。似乎意图是 LDFLAGS 用于 -Wl,--something.

之类的东西