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的區別


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM