Reputation: 45
Here's my code.
#include <thread>
#include <iostream>
//#include <ctime>
using namespace std;
void f() {}
int main() {
//struct timespec t;
//clock_gettime(CLOCK_REALTIME, &t);
thread(f).join();
return 0;
}
I have tried to compile this program in the following ways:
1. g++ a.cc -o a -std=c++11 -pthread
2. g++ a.cc -o a -std=c++11 -lpthread
3. g++ -pthread a.cc -o a -std=c++11
4. g++ a.cc -c -std=c++11 && g++ a.o -o a -lpthread
5. g++ a.cc -c -std=c++11 -pthread && g++ a.o -o a -pthread
All of them can output the executable "a". However, none of them seem to link the library "libpthread.so":
./a
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Aborted
ldd ./a
linux-vdso.so.1 => (0x00007fff997fe000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f350f7b5000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f350f59f000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f350f1d6000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f350eed2000)
/lib64/ld-linux-x86-64.so.2 (0x00007f350fad4000)
But, the following way works perfectly:
uncomment all the three lines in the above code(must do this, or the same error appears);
using "g++ a.cc -std=c++11 -o a -lrt".
I'm using ubuntu 13.10. Any reason for this?
Upvotes: 1
Views: 959
Reputation: 181077
This would seem to be a known Ubuntu related bug.
You can (as noted on the linked page) work around it by passing -Wl,--no-as-needed
on the command line;
> g++ a.cc -pthread -std=c++11
> ./a.out
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
>
> g++ a.cc -pthread -std=c++11 -Wl,--no-as-needed
> ./a.out
>
Upvotes: 1