What are the correct link options to use std::thread in GCC under linux?(在 linux 下的 GCC 中使用 std::thread 的正确链接选项是什么?)
问题描述
我正在尝试将 std::thread 与 G++ 一起使用.这是我的测试代码
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
我的编译器版本:
$ 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
我试过了
$ g++ -std=c++0x -lpthread test.cpp
$ ./a.out
还是一样.
推荐答案
我觉得在Linux上pthread是用来实现std::thread的,所以需要指定-pthread代码>编译器选项.
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 的正确链接选项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 linux 下的 GCC 中使用 std::thread 的正确链接选项
基础教程推荐
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- c++ STL设置差异 2022-01-01
