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
Your code may not have a pthread_create in it but just think for a minute what asynchronous processing actually entails.
It's a near certainty that an asynchronous function will be run in a separate thread so that it can, well, run asynchronously with the main code. That should be evident by the fact your error message contains the text std::thread several times.
And, in your environment, it seems that std::thread uses pthreads under the covers.
This is really no different to your code containing cout << someInt and it possibly complaining about a missing itoa-like function for doing integer-to-string conversions. Your setup would probably be seriously broken if it included streams but no standard conversions but the concept is still valid.
The executable must have access to all the stuff you explicitly call, plus all of the other dependencies that entails.
For what it's worth, this is covered (albeit vaguely) in the gcc docs (my emphasis):
When you link a multithreaded application, you will probably need to add a library or flag to
g++. This is a very non-standardized area of GCC across ports. Some ports support a special flag (the spelling isn't even standardized yet) to add all required macros to a compilation (if any such flags are required then you must provide the flag for all compilations not just linking) and link-library additions and/or replacements at link time. The documentation is weak.On several targets (including GNU/Linux, Solaris and various BSDs)
-pthreadis honored.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With