samedi 17 février 2018

std::async with gcc 7.2 need pthread link option

The following code need pthread link option to compile, and i don't understand why. Do you have any idea?

I'm using gcc 7.2.0

#include <future>

int sum = 0;

void func()
{
   for(int i=0; i < 10; ++i)
      sum +=i;
}

int main()
{
    std::future<void> f =  std::async(std::launch::async, func);
    return 0;
}

Compiling with

g++ -o test test.cpp

provides the following error

/tmp/ccoEkyeZ.o: dans la fonction « std::thread::thread<std::__future_base::_Async_state_impl<std::thread::_Invoker<std::tuple<void (*)()> >, void>::_Async_state_impl(std::thread::_Invoker<std::tuple<void (*)()> >&&)::{lambda()#1}>(std::__future_base::_Async_state_impl<std::thread::_Invoker<std::tuple<void (*)()> >, void>::_Async_state_impl(std::thread::_Invoker<std::tuple<void (*)()> >&&)::{lambda()#1}&&) »:
test.cpp:(.text._ZNSt6threadC2IZNSt13__future_base17_Async_state_implINS_8_InvokerISt5tupleIJPFvvEEEEEvEC4EOS8_EUlvE_JEEEOT_DpOT0_[_ZNSt6threadC5IZNSt13__future_base17_Async_state_implINS_8_InvokerISt5tupleIJPFvvEEEEEvEC4EOS8_EUlvE_JEEEOT_DpOT0_]+0x30): référence indéfinie vers « pthread_create »
collect2: error: ld returned 1 exit status

while using

g++ -o test test.cpp -lpthread

has no issue.

is this normal? do i missing something? my code doesn't have pthread_create somewhere

Aucun commentaire:

Enregistrer un commentaire