kal*_*ida 5 c linux multithreading linker-errors
我正在尝试构建我的程序,但不断收到相同的错误消息:
undefined reference to pthread_create
undefined reference to pthread_join
Run Code Online (Sandbox Code Playgroud)
我已包含 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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在抱怨的地方调用 create 和 join 的地方。可能是什么问题呢?
用于构建的 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
Run Code Online (Sandbox Code Playgroud)
mfind是主程序,list.c也是一个已实现的列表。
-pthread不属于CFLAGS. 正如您已经发现的,所需的库必须位于命令行选项的末尾,位于所有目标文件之后。你需要-pthread输入LDLIBS因为你有 makefile,所以
来自https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html:
\n\n\n低密度脂蛋白IBS
\n当编译器调用链接器时为编译器提供的库标志或名称 \xe2\x80\x98ld\xe2\x80\x99。LOADLIBES 是 LDLIBS 的替代品,已弃用(但仍受支持)。非库链接器标志(例如 -L)应放入 LDFLAGS 变量中。
\n