未定义的参考`pthread

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

对`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 -Wswitch-default -Wunreachable-code。-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

在编译单个翻译单元时,不需要在连接阶段使用-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
链接地址: http://www.djcxy.com/p/73607.html

上一篇: undefined reference to `pthread

下一篇: How does this code print 404?