对'pthread_create'的未定义引用-pthread不起作用

Cow*_*s42 2 c pthreads

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

对'pthread_create'的未定义引用

我用来编译的标志如下:

CFLAGS = -std = gnu99 -pthread -g -Wall -Wextra -Werror -Wmissing-声明-Wmissing的原型-Werror隐函数声明-Wreturn型-Wparentheses -Wunused -WOLD式清晰度-Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code

编译器是gcc

Makefile文件:

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
Run Code Online (Sandbox Code Playgroud)

小智 5

-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
Run Code Online (Sandbox Code Playgroud)