pthread的库不是Linux系统的库,所以在使用gcc或g++进行编译的时候要加上-lpthread
如
g++ -o test main.cpp -lpthread
在qt中使用时也要将库添加进去,如果不添加会出现undefined reference错误。如下:
/home/qt-project/First_qt/main.cpp:18: error: undefined reference to `pthread_create'
/home/qt-project/First_qt/main.cpp:25: error: undefined reference to `pthread_join'
:-1: error: collect2: error: ld returned 1 exit status
添加方法,打开.pro文件,在后面添加一行
注:我用的gmake
LIBS += -lpthread
如果库没包含在默认目录那么就这么加
//库路径:/home/pthreadlib/Pre-built.2/lib
//在QT中指定库的路径:
LIBS += -L/home/pthreadlib/Pre-built.2/lib -lpthread