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

时间:2018-11-08 20:15:47

标签: c makefile ld ldflags

这就是发生的事情。

$ 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。如何确保链接器的提示转到命令行末尾?

1 个答案:

答案 0 :(得分:5)

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之类的东西。