来自c ++ 11的std :: thread问题 [英] Issue with std::thread from c++11

查看:88
本文介绍了来自c ++ 11的std :: thread问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从标准模板库编译具有多线程的程序时遇到一些麻烦.当我尝试编译以下程序时,它返回给我一个晦涩的错误:

I have some troubles trying to compile a program with multi-threading from the standard template library. It return me a obscure error when i try to compile the following program :

#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

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

有人可以帮我吗?

推荐答案

-pthread 传递给编译器.此标志结合了编译和链接pthread库所需的内容( -lpthread 并不总是足够的).参见此问题.

Pass -pthread to the compiler. This flag combines what is necessary to compile and link the pthread library (-lpthread is not always enough). See this question.

这篇关于来自c ++ 11的std :: thread问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆