[C++]记在Linux下使用std::thread的错误

本文介绍了在VS2017中通过远程连接到Linux服务器进行C++开发时遇到的一个多线程问题及解决方案。当使用std::thread时,在运行阶段会报错“Operation not permitted”。解决方法是在编译和链接时加入-pthread参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述

VS2017 使用Visual Studio For Linux远程连接到T家的云服务器上进行开发。
在某个地方用到了C++11标准之后的std::thread.
编译正常,在运行时遇到了错误:

│terminate called after throwing an instance of 'std::system_error'
│what():  Enable multithreading to use std::thread: Operation not permitted

服务器使用g++编译,版本如下:

g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28)                                                                                        │
Copyright (C) 2015 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.

解决办法

解决方法很简单,如果不使用vs2017,在编译链接的同时指定参数-pthread即可。
例如:

g++ -c main.cpp -pthread -std=c++11         // 编译
g++ main.o -o main.out -pthread -std=c++11  // 链接

如果使用vs2017,那么可以在下述位置添加-pthread选项。

这里写图片描述

不要忘记了链接属性也要加入该选项:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值