Linux c+的奇怪行为+;gcc 4.8.1中的11螺纹
这是我的密码Linux c+的奇怪行为+;gcc 4.8.1中的11螺纹,linux,multithreading,gcc,c++11,Linux,Multithreading,Gcc,C++11,这是我的密码 #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; } 它们都可以输出可执行
#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;
}
它们都可以输出可执行文件“a”。但是,它们似乎都没有链接库“libpthread.so”:
但是,以下方法非常有效:
-Wl,--no来绕过它
> 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
>
您可以(如链接页面上所述)通过在命令行上按需传递-Wl,--no来绕过它
> 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
>
如果您使用g++-4.8-std=c++11-Wall a.cc-lpthread-oa
(在Debian/Sid上使用GCC4.8.2),您的程序运行正常(并且没有显式的-lpthread
我会得到与您相同的错误)。如果您使用g++-4.8-std=c++11-Wall a.cc-lpthread oa
(在Debian/Sid上使用GCC4.8.2)进行编译您的程序运行正常(如果没有显式的-lpthread
,我将得到与您相同的错误)。
> 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
>