gpt4 book ai didi

c - 对 `pthread_create' 的 undefined reference -pthread 不工作

转载 作者:行者123 更新时间:2023-12-03 23:29:28 29 4
gpt4 key购买 nike

我正在函数中创建新线程,并且我已包含 pthread.h。但它不起作用,我在编译时不断收到以下错误:

undefined reference to `pthread_create'

我用来编译的标志如下:

CFLAGS=-std=gnu99 -pthread -g -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused -Wold-style-definition -Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code

编译器是gcc

生成文件:

CC=gcc
CFLAGS=-std=gnu99 -pthread -g -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused -Wold-style-definition -Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code

all: finder

finder: stack.o list.o finder.o
$(CC) -o mfind stack.o list.o mfind.o

stack.o: stack.c stack.h
$(CC) -c stack.c $(CFLAGS)

list.o: list.c list.h
$(CC) -c list.c $(CFLAGS)

finder.o: finder.c finder.h
$(CC) -c finder.c $(CFLAGS)

clean:
rm -f *.o finder

最佳答案

-pthread 在链接阶段需要,而不是在编译各个翻译单元时。典型的方法如下所示:

CC=gcc
CFLAGS=-std=gnu99 -g -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused -Wold-style-definition -Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code
LIBS=-pthread

all: finder

finder: stack.o list.o finder.o
$(CC) -o mfind stack.o list.o mfind.o $(LIBS)

stack.o: stack.c stack.h
$(CC) -c stack.c $(CFLAGS)

list.o: list.c list.h
$(CC) -c list.c $(CFLAGS)

finder.o: finder.c finder.h
$(CC) -c finder.c $(CFLAGS)

clean:
rm -f *.o finder

关于c - 对 `pthread_create' 的 undefined reference -pthread 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46732407/

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