2016-03-13 34 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