简体   繁体   中英

c++11 future compilation error

For the following code written in c++11 and 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

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. To use the Pthreads functions you need to link to libpthread, so add -pthread to your compiler and linker commands.

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. That function is provided by libpthread, so you need to link to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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