在Linux下的GCC中使用std :: thread的正确链接选项是什么? [英] What are the correct link options to use std::thread in GCC under linux?
问题描述
您好我正在尝试在G ++中使用 std :: thread
。这里是我的测试代码
#include< thread>
#include< iostream>
int main(int,char **){
std :: thread tt([](){std :: cout<Thread!<<< std: :endl;});
tt.join();
}
它编译,但是当我尝试运行它时,结果如下:
在抛出'std :: system_error'的实例后调用terminate
what():不允许的操作
Aborted
我的编译器版本:
$ g ++ --version
g ++(Ubuntu / Linaro 4.6.1-9ubuntu3)4.6.1
Copyright(C)2011 Free Software Foundation,Inc.
这是免费软件;请参阅复制条件的来源。没有任何b $ b保修;甚至不适用于适销性或针对特定用途的适用性。
我的测试代码出了什么问题?
更新:我使用以下命令行来编译和运行我的代码。
$ g ++ -std = c ++ 0x test.cpp
$ ./a.out
我试过了
$ g ++ -std = c ++ 0x -lpthread test.cpp
$ ./a.out
仍然是相同的。
更新:以下编译命令有效。 / p>
$ g ++ -std = c ++ 0x test.cpp -lpthread
$ ./a.out
线程!
我想在Linux上用pthread来实现 std :: thread
所以你需要指定 -pthread
编译器选项。
因为这是一个链接选项,所以这个编译器选项必须是 AFTER 源文件:
$ g ++ -std = c ++ 0x test.cpp -pthread
Hi I am trying to use std::thread
with G++. Here is my test code
#include <thread>
#include <iostream>
int main(int, char **){
std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
tt.join();
}
It compiles, but when I try to run it the result is:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted
My compiler version:
$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
What is wrong with my test code?
UPDATE: I use the following command line to compile and run my code.
$ g++ -std=c++0x test.cpp
$ ./a.out
and I tried
$ g++ -std=c++0x -lpthread test.cpp
$ ./a.out
still the same.
UPDATE: the following compile command works.
$ g++ -std=c++0x test.cpp -lpthread
$ ./a.out
Thread!
I think on Linux pthread is used to implement std::thread
so you need to specify the -pthread
compiler option.
As this is a linking option, this compiler option need to be AFTER the source files:
$ g++ -std=c++0x test.cpp -pthread
这篇关于在Linux下的GCC中使用std :: thread的正确链接选项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!