2016-03-13 113 views
1

我遇到了一點奇怪的行爲,當我嘗試編譯使用多線程在CodeLite任何程序。C++ CodeLite啓用多線程?

我得到的錯誤:

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

一些快速google搜索,我發現我不得不添加「-pthread」編譯器選項之後。

Image of Codelite Options. Linker Options

注意:CodeLite把-l在圖書館前面,所以它並使用-lpthread

我乾淨後,重建項目,我仍然得到錯誤,但。據我可以告訴構建日誌看起來不錯?

Image of CodeLite build log.

而真正令人沮喪的說到,當我通過命令行手動編譯它,它工作得很好。

Compiling manually works

我已經搜查,但沒有解決方案,似乎爲我工作?也許我錯過了某個地方的某個步驟?

這裏是我的測試代碼。 我也應該注意到我使用Ubuntu14.04和CodeLite 9.1.0

#include <iostream> 
#include <string> 

#include <thread> 

void test() 
{ 
    std::cout << " Look it works! \n"; 
} 

void int main(int argc, char** argv) 
{ 
    std::thread thrd_1 = std::thread(test); 
    thrd_1.join(); 

    return 0; 
} 

任何幫助將不勝感激!

+0

您正在鏈接到IDE中的pthread?我使用:'-pthread -lpthread輪候冊, - 無作爲,needed'。您的命令行有'-lpthread'但你對你的IDE的截屏不顯示連接選項。 – Brandon

+0

@Brandon啊,對不起,我也在鏈接'-lpthread'我會做一個快速編輯。我已經嘗試過'-Wl, - 不需要'作爲編譯器選項了?也許我把它放在錯誤的地方? – Dusty

回答

1

您傳遞編譯器選項-pthread。你需要 通過它的連接選項,並在連接選項你並不需要 指定pthread爲庫。該-pthread選項意味着 做什麼,那就是連接這個平臺對POSIX線程庫。

$ g++ -c -O0 -std=c++11 -o main.o main.cpp 
$ g++ -o threadtest -pthread main.o 
$ ./threadtest 
Look it works! 
+0

完美!謝謝,我很高興它非常簡單! – Dusty