C++:使用线程类时对“pthread_create”的未定义引用

问题描述

我在 C++ 中遇到多线程问题。这是我的代码

#include <iostream>
#include <thread>

bool stop = false;

void loop() {
    while (!stop) {
        // Do something...
    }
}

int main() {
    std::thread t(loop);
    
    std::cin.get();
    stop = true;
    t.join();

    std::cout << "\nPress any key to continue";
    std::cin.get();

    return 0;
}

我想要完成的是在按下某个键后立即跳出 loop() 中的循环。当我尝试编译时,它抛出一个错误

$ sudo g++ main.cpp 
/usr/bin/ld: /tmp/ccZOsb6T.o: in function `std::thread::thread<void (&)(),void>(void (&)())':
main.cpp:(.text._ZNSt6threadC2IRFvvEJEvEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEvEEOT_DpOT0_]+0x20): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

我做错了吗?感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)