即使使用 lpthread 也未定义对"pthread_join"错误的引用

Having undefined reference to `pthread_join' error even using lpthread

本文关键字:join 引用 pthread 错误 lpthread 未定义      更新时间:2023-10-16

我在ubuntu上使用linux。然而,我甚至在使用pthread和lpthread时也遇到了问题。请帮忙!提前谢谢!

yuki@ubuntu:~/NetBeansProjects/csci212A3$ g++ Path.o Maze.o SubmitMazeSoln.o TestSubmitMazeSoln.cpp -o -lpthread
In file included from Maze.h:12:0,
             from TestSubmitMazeSoln.cpp:11:
Assignm3_Utils.h: In constructor ‘Point::Point()’:
Assignm3_Utils.h:17:19: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
Point ()    { x = NULL; y = NULL; }
               ^
Assignm3_Utils.h:17:29: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
Point ()    { x = NULL; y = NULL; }
                         ^
/tmp/ccMQbyoO.o: In function `newThread()':
TestSubmitMazeSoln.cpp:(.text+0x3ab2): undefined reference to `pthread_create'
TestSubmitMazeSoln.cpp:(.text+0x3b0a): undefined reference to `pthread_create'
TestSubmitMazeSoln.cpp:(.text+0x3b53): undefined reference to `pthread_join'
TestSubmitMazeSoln.cpp:(.text+0x3b79): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status

这真的是命令行的准确副本吗:

g++ Path.o Maze.o SubmitMazeSoln.o TestSubmitMazeSoln.cpp -o -lpthread

-o选项指定输出文件,并需要一个参数。如上所述,这个自变量是-lpthread。因此-lpthread而不是参数(并且不会搜索pthread库);它是输出文件的名称。(你并不真的想要一个名为-lpthread的可执行文件或任何文件;在Unix下,以-开头的文件名会带来很多问题。)

关于警告:我猜Point中的xy的类型为intNULL是指定空指针的传统方式,将其用作int在第一程度上是混淆。因此g++发出警告。(当然,从C++11开始,应该更喜欢nullptr作为空指针,而不是NULL。)