我正在函数中创建新线程,并且我已包含 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/