我写过以下T课程pthread.当我使用g ++ -lpthread编译这个类时,它工作正常.但是如果我从另一个类扩展这个类A并一起编译它会返回一个错误; "对pthread_cancel的未定义引用"
码:
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();
}
};
Run Code Online (Sandbox Code Playgroud)
下一堂课:
class A:T{
A(){
start();
}
}
Run Code Online (Sandbox Code Playgroud)
主要
int main(){
A a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译:
g++ -c T.cpp A.cpp Main.cpp -lpthread
g++ -o out *.o
Run Code Online (Sandbox Code Playgroud)
错误: 未定义对`pthread_cancel'的引用
Mat*_*Mat 11
改为:
g++ -pthread -c T.cpp A.cpp Main.cpp
g++ -pthread -o out *.o
Run Code Online (Sandbox Code Playgroud)
-lpthread是一个链接器标志,它仅在链接时使用,而不是在编译时使用,所以你所拥有的不正确 - 链接部分在第二步中发生.
并且一般不要使用-lpthread.使用-pthread它们进行编译和链接.
从GCC手册:
使用pthreads库添加对多线程的支持.此选项为预处理器和链接器设置标志.
| 归档时间: |
|
| 查看次数: |
4043 次 |
| 最近记录: |