使用c++线程获得错误

Get an error using c++ threads

本文关键字:错误 线程 c++ 使用      更新时间:2023-10-16

我正在编写一个程序,它通过使用一个线程来改变许多窗口的焦点。

pthread_t getFocus;
pthread_create ( &getFocus, NULL, returnFocus, NULL );
pthread_join ( getFocus, NULL );
void *returnFocus ( void *argument ) {
    return 0;
}

编译时,我得到这个错误:

undefined reference to _imp__pthread_create 
undefined reference to _imp__pthread_join

我应该怎么做来纠正这个错误?

当使用using pthread时,您还需要使用-lpthread选项链接到pthread库。

gcc -o myprog myprog.c -lpthread
编辑:

正如Maxim在评论中所说,这样做的可移植方法是:

gcc -o myprog myprog.c -pthread

-lpthread是Unix的方法(我学过的),但您应该始终使用-pthread,如果不可用(或者如果您有非常特殊的原因),请使用-lpthread

在linux上-lpthread表示"与pthread库的链接",而-pthread只是说"做你需要为pthread做的事情",从我读到的