環境說明
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之后引入的內容。
- -lpthread是老式的寫法(有可能在所在環境還是會報錯),推薦用-pthread。參見gcc編譯選項-lpthread和-pthread的區別