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

标签 c pthreads

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

相关文章:

c++ - 如何在不使用mutex、semorphore、spinLock、futex的情况下实现线程同步?

c++ - 类中的pthread_create调用函数

c - Pthreads - 为什么 C 函数被声明为 void*?

linux - 更改另一个线程的 pthread 取消类型?

C++_win32 : pthread_self() == pthread_t leading to error

java - 在Blackberry中集成C和JAVA

c - 如何计算C中的 float ?

c - 生成不同的随机数

c - 使用 scanf 读取 C 中一定数量的字符?

c - 使用 OpenGL/C 生成 N x N 网格以显示在窗口中心