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

注意:

  1. 老版本g++编译器,需要同时指定使用的C++标准,因为老版本编译器默认不是支持C++11的,而thread类是C++11之后引入的内容。
  2. -lpthread是老式的写法(有可能在所在环境还是会报错),推荐用-pthread。参见gcc编译选项-lpthread和-pthread的区别
原文地址:https://www.cnblogs.com/fortunely/p/15470543.html