gpt4 book ai didi

c++ - G++ 命令行使用线程标记,linux makefile 不使用

转载 作者:太空狗 更新时间:2023-10-29 21:17:03 26 4
gpt4 key购买 nike

此命令行在 ubuntu 上运行良好(使用 C++ 和线程):

g++ -std=c++11 prog.cpp -o prog.out -lpthread

我的 makefile 爆炸了:

all: main

main: prog.o
g++ -o prog prog.o

prog.o: prog.cpp
g++ -std=c++11 -c prog.cpp -lpthread

我不确定,但看起来 -lpthread 标志没有被选中。已经很晚了,我已经在 makefile 上工作了两个小时,如有任何帮助,我们将不胜感激。

make 返回一个错误:对'pthread_create' 的 undefined reference

最佳答案

你忘了解释“blows up”的意思。

但大概它的意思是“链接失败”,那是因为你把它放在了错误的命令上。

-l 是一个链接器 标志,但您在编译 命令中有它。

您需要将它移动到 main 目标。

你还记下了make rule #2 (来自 GNU make 维护者):

Every non-.PHONY rule must update a file with the exact name of its target.

Make sure every command script touches the file “$@“–not “../$@“, or “$(notdir $@)“, but exactly $@. That way you and GNU make always agree.

如果您想利用 built-in rules,您还可以大大简化您的 makefile(基本上什么都不做) .

CPPFLAGS := -std=c++11
LDLIBS := -lpthread

all: prog

就是这样。

关于c++ - G++ 命令行使用线程标记,linux makefile 不使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33817701/

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