c - 为什么 LDFLAGS 将我的库放在生成 undefined reference 的目标文件之前?

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

最佳答案

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

关于c - 为什么 LDFLAGS 将我的库放在生成 undefined reference 的目标文件之前?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53215476/

相关文章:

c - 如何让输入在某个点被切断或环绕?

c - 在C中逐行读取文件(不使用fgets)

c++ - C++ makefile 问题

linux - 在自定义工具中使用 traceroute 时出现编译问题

c - 直接调用动态链接器时确定真正的可执行文件

c - 目标文件到二进制代码

c - 调试 R 包中的 c 函数

c - 读取值并将其存储在主变量中时出现问题吗?

c++ - CLIon - 使用 SDL_TTF 的 undefined reference

linux - 从 _start 调用 printf 时出现链接器错误