【发布时间】:2017-10-15 00:04:31
【问题描述】:
我正在尝试构建我的程序,但不断收到相同的错误消息:
undefined reference to pthread_create
undefined reference to pthread_join
我已包含 pthread.h 并在我的 makefile 中使用 -pthread 编译。
int threadAmount = strtol(nrthr, NULL, 0);
threadAmount--;
if(threadAmount > 0){
pthread_t tids[threadAmount];
for(int i = 0;i < threadAmount; i++){
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tids[i],&attr,search,&t1);
}
for(int i = 0;i < threadAmount; i++){
pthread_join(tids[i],NULL);
}
}
这就是我在抱怨的地方创建和加入的地方。可能是什么问题?
用于构建的makefile:
CC=gcc
CFLAGS= -pthread -std=gnu11 -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: mfind
list.o: list.c list.h
$(CC) -c list.c $(CFLAGS)
mfind.o: mfind.c list.h
$(CC) -c mfind.c $(CFLAGS)
mfind: mfind.o list.o
$(CC) mfind.o list.o -o mfind
clean:
rm -f *.o mfind
mfind 是主程序,list.c 是一个实现的列表。
【问题讨论】:
-
您的建设情况如何?编辑您的问题并添加该信息。这是一个链接器错误,pthread 库没有以某种方式链接
-
我在makefile中添加了,够了吗? @yano
-
链接时的顺序很重要。尝试从
CFLAGS中删除-pthread并执行$(CC) mfind.o list.o -pthread -o mfind。通常有编译器标志(您的 CFLAGS)和链接器标志(-pthread将进入链接器标志变量,编译器不应该需要它)。但是,我不是 makefile 专家。 -
甜蜜!一般来说,如果
codeA.o调用codeB.o中的函数,那么您必须在链接器命令中与codeA.obeforecodeB.o链接.. 反正这是我的经验。 -
-pthread会影响预处理器宏和链接,因此最好始终将其指定给“gcc”驱动程序。
标签: c linux multithreading linker-errors