来自 c++11 的 std::thread 的问题

Issue with std::thread from c++11

本文关键字:thread 问题 std c++11 来自      更新时间:2023-10-16

我在尝试从标准模板库中编译具有多线程的程序时遇到了一些麻烦。当我尝试编译以下程序时,它会返回一个模糊的错误:

#include <iostream>
#include <thread>
void foo()
{
    std::cout << "Thread 1n";
}
int main(int argc, char** argv)
{
    std::thread tr(foo);
    std::cout << "Main threadn";
    tr.join();
    return 0;
}

我不明白错误:

/tmp/ccE8EtL1.o : In the function « std::thread::thread<void (&)()>(void (&)()) » :
 file.cpp:(.text._ZNSt6threadC2IRFvvEJEEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEEEOT_DpOT0_]+0x21) : undefined reference to « pthread_create »
  collect2: error : ld has return 1 execution status code

我用:

g++ -std=c++14 file.cpp -o test -Wall

谁能帮我?

-pthread传递给编译器。这个标志结合了编译和链接 pthread 库所必需的东西(-lpthread并不总是足够的(。看到这个问题。