Linux教程

Linux C/C++ 编译、调试问题汇总

本文主要是介绍Linux C/C++ 编译、调试问题汇总,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

环境说明

Ubuntu版本: 14.04.1 x86_64 LTS
g++ 版本: 4.8.4

问题

terminate called after throwing an instance of 'std::system_error'

g++编译cpp源码后,运行程序出现错误提示:“terminate called after throwing an instance of 'std::system_error'”

$ gcc -std=C++11 test.cpp -o test
$ ./test
terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
已放弃 (核心已转储)

问题原因:使用了C++ thread,但是没有链接pthread线程库。
解决办法:修改编译命令,链接pthread库

$ g++ -std=C++11 test.cpp -o test -lpthread
// 或者
$ g++ -std=C++11 test.cpp -o test -pthread

注意:老版本g++编译器,需要同时指定使用的C++标准,因为老版本编译器默认不是支持C++11的,而thread类是C++11之后引入的内容。

这篇关于Linux C/C++ 编译、调试问题汇总的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!