gpt4 book ai didi

makefile - 对 "timer_create"的 undefined reference ,即使 "-lrt"包含编译

转载 作者:行者123 更新时间:2023-12-02 10:57:50 26 4
gpt4 key购买 nike

我正在处理的项目的 Makefile 有一些问题。我得到“对'timer_create'的 undefined reference ”等,即使它们包含在linkopts中。我认为问题在于这些库位于编译行的前面而不是最后,但我对这样的 Makefile 非常不熟悉。如何确保链接出现在末尾而不是开头?这是我正在谈论的 Makefile 的一部分,make,它尝试这样做:
gcc -g -lpthread -lrt -Wall -o scheduler scheduler.o worker.o list.o smp5_tests.o testrunner.o
但我很确定应该是这样的:

gcc -g -Wall -o scheduler scheduler.o worker.o list.o smp5_tests.o testrunner.o -lpthread -lrt

这是生成文件:
CC = gcc
CCOPTS = -c -g -Wall
LINKOPTS = -g -lpthread -lrt -Wall

EXEC=scheduler
OBJECTS=scheduler.o worker.o list.o smp5_tests.o testrunner.o

all: $(EXEC)

$(EXEC):$(OBJECTS)
$(CC) $(LINKOPTS) -o $@ $^

最佳答案

I think the issue is that the libraries are at the front of the compile line instead at the end, but I am pretty unfamiliar with a Makefile like this.



嗯,这是特定于 GNU ld 链接器的,而不是自己制造的: ld 从左到右一次解析依赖关系,除了 -Wl,--start-group 之间。和 -Wl,--end-group (专门用于 handling circular dependencies )。这意味着库必须放在使用它们的模块(或其他库)之后。

How can I ensure the links come at the end instead of at the beginning?



考虑如何使用默认规则(的简化版本)完成:
%: %.o
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

这里 LDFLAGS 是“正常的”链接器标志,可以安全地位于对象列表之前; LDLIBS 是程序使用的系统库列表。

关于makefile - 对 "timer_create"的 undefined reference ,即使 "-lrt"包含编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55521375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com