简体   繁体   English

c++11未来编译错误

[英]c++11 future compilation error

For the following code written in c++11 and c++14对于以下用 c++11 和 c++14 编写的代码

#include <iostream>
#include <vector>
#include <future>


using namespace std;


int main()
{
    vector < future<int> > futures;
    for(int i =0; i<10; i++)
    {
        futures.push_back( async([](auto j){return j*2;} ,i));
    }

for(auto &e : futures)
{
    cout << e.get() << endl;
}
return 1;
}

I am getting the following error:我收到以下错误:

/tmp/ccO0BfSt.o: In function `_ZNSt6threadC2IZNSt13__future_base17_Async_state_implISt12_Bind_simpleIFZ4mainEUliE_iEEiEC4EOS6_EUlvE_IEEEOT_DpOT0_': c14_features.cpp:(.text+0x1d92): undefined reference to `pthread_create' collect2: error: ld returned 1 exit status /tmp/ccO0BfSt.o:在函数`_ZNSt6threadC2IZNSt13__future_base17_Async_state_implISt12_Bind_simpleIFZ4mainEUliE_iEEiEC4EOS6_EUlvE_IEEEOT_DpOT0_'中:c14_features.cpp:1:(.9ld_text) 返回的线程错误:(.9ld+20)

Any idea why I am getting this??知道为什么我会得到这个吗?

GCC's C++11 thread library is built on top of the native thread support, which usually means Pthreads. GCC 的 C++11 线程库建立在原生线程支持之上,通常是指 Pthreads。 To use the Pthreads functions you need to link to libpthread, so add -pthread to your compiler and linker commands.要使用 Pthreads 函数,您需要链接到 libpthread,因此将-pthread添加到您的编译器和链接器命令中。

NB that's not technically a compilation error, it's a linking error.注意,这在技术上不是编译错误,而是链接错误。 The file was compiled OK, but could not be linked because nothing in the program provides the pthread_create function.该文件编译正常,但无法链接,因为程序中没有提供pthread_create函数。 That function is provided by libpthread, so you need to link to it.该函数由 libpthread 提供,因此您需要链接到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM