c++11中std::thread的问题

7

我尝试使用标准模板库进行多线程编译时遇到了一些问题。 当我尝试编译以下程序时,它会返回一个晦涩的错误:

#include <iostream>
#include <thread>

void foo()
{
    std::cout << "Thread 1\n";
}

int main(int argc, char** argv)
{
    std::thread tr(foo);
    std::cout << "Main thread\n";
    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

请问有人可以帮我吗?
1个回答

16

谢谢,这就是解决方案 \o/ - Harry333Cover
完整性考虑:[g++ - 使用std :: thread时为什么必须传递“-pthread”选项?] (https://dev59.com/eVYN5IYBdhLWcg3wuaNx) - user

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接