[英]undefined reference to `pthread_cancel'
I have written the following T
class with pthread
. 我用
pthread
编写了以下T
类。 When i compile this class using g++ -lpthread then it's working fine. 当我使用g ++ -lpthread编译这个类时,它工作正常。 But if i extend this class from another class
A
and compile all together it's returns an error; 但是如果我从另一个类
A
扩展这个类并一起编译它会返回一个错误; "undefined reference to pthread_cancel" “对pthread_cancel的未定义引用”
Code: 码:
class T{
private:
pthread_t thread;
public:
void start(){
pthread_create(&thread,NULL,&run,this);
}
void destroy_thread(){
pthread_cancel(thread);
}
static void* run(void*){}
~Thread(){
destroy_thread();
}
};
Next class: 下一堂课:
class A:T{
A(){
start();
}
}
Main 主要
int main(){
A a;
return 0;
}
Compile: 编译:
g++ -c T.cpp A.cpp Main.cpp -lpthread
g++ -o out *.o
Error: undefined reference to `pthread_cancel' 错误: 未定义对`pthread_cancel'的引用
Do this instead: 改为:
g++ -pthread -c T.cpp A.cpp Main.cpp
g++ -pthread -o out *.o
-lpthread
is a linker flag, it's used only when linking, not compiling, so where you have it isn't correct - the linking part happens in the second step. -lpthread
是一个链接器标志,它仅在链接时使用,而不是在编译时使用,所以你所拥有它的地方不正确 - 链接部分在第二步中发生。
And generally don't use -lpthread
anyway. 并且通常不要使用
-lpthread
。 Use -pthread
both for compiling and linking. 使用
-pthread
进行编译和链接。
From the GCC manual: 从GCC手册:
Adds support for multithreading with the pthreads library.
使用pthreads库添加对多线程的支持。 This option sets flags for both the preprocessor and linker.
此选项为预处理器和链接器设置标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.