Eclipse和c ++ 11中的线程 [英] Threads in Eclipse AND c++11

查看:82
本文介绍了Eclipse和c ++ 11中的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标一直是创建多线程程序,我什至无法获得一个简单的线程 在ON ECLIPSE CDT上执行.我的工具: ECLIPSE 3.8.1 CDT Ubuntu 13.10

My goal has been to create multi-threading programs, and I can not even get a simple thread to execute ON ECLIPSE CDT. my Tools: ECLIPSE 3.8.1 CDT Ubuntu 13.10

我已经注意到与我非常相似的问题.我尝试了其他解决方案,但无法让它们为我工作.

I have noticed very similar issues regarding mine. I have tried those other solutions but I could not get them to work for me.

当我在Eclipse CDT中键入代码时,Eclipse不会解析"符号"thread",但是,它可以找到头文件"thread". "Mutex"也无法解析.此外,构建之后,我运行该程序,eclipse返回:

When I type the code in Eclipse CDT, Eclipse does not 'resolve' the symbols 'thread', however, It can find the header file 'thread'. 'Mutex' also does not resolve. Furthermore, after building, I run the program, eclipse returns :

"终止在引发"std :: system_error"实例后调用 what():启用多线程以使用std :: thread:不允许进行操作"

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

一些附加说明:

我可以使用以下命令在终端中编译和执行代码: 'clang ++ c.cpp -pthread -std = c ++ 11' 但... 'g ++ c.cpp -pthread -std = c ++ 11'编译并 产生与上面引用的相同的错误.因此,这似乎是一个编译器问题.我做了 开始使用clang ++编译器在Eclipse CDT中的新项目中编写代码,现在给出相同的未解决的线程"并产生上述错误.所以现在我想我在Eclipse中设置了一些错误的设置,路径或标志.

I can compile and execute the code in the terminal using: 'clang++ c.cpp -pthread -std=c++11' but... 'g++ c.cpp -pthread -std=c++11' compiles and produces the same error as quoted above. So it looks like it's a compiler issue. I did start to write the code in a new project within Eclipse CDT with the clang++ compiler and now that gives the same non-resolved 'thread' and produces the error as quoted above. So now I think I have some wrong settings, paths or flags set in Eclipse.

include <iostream>
include <thread>

using namespace std;
void p1(){
    cout<<"process 1 is processing"<<endl;
}

int main() {
    thread t1(&p1);

    cout<<"Hello from main()"<<endl;
    t1.join();
    return 0;
}

推荐答案

我一直在努力解决同样的问题,终于解决了.这是我所做的:

I have been struggling with the very same issue and I finally resolved it. Here is what I did:

1)为c ++构建添加-std = c ++ 11.为此,请右键单击您的项目,选择属性,然后:C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous(last option)

1) Add -std=c++11 for c++ build. To do that right-click your project, select properties and then: C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous(last option)

在其他标记中附加-std=c++11.我的其他标志现在看起来像:-c -fmessage-length=0 -std=c++11,但您的标志可能有所不同.

In other flags append -std=c++11. My Other flags now looks like: -c -fmessage-length=0 -std=c++11 but yours may be a bit different.

2)添加一些链接器选项.在与上述相同的视图(C/C ++ Build-> Settings)中,选择GCC C ++ Linker选项,然后从此处转到Miscellaneous(倒数第二个选项).添加以下链接器标志(该字段对我来说是空的):-Wl,--no-as-needed -pthread.点击应用.

2) Add some linker options. In the same view(C/C++ Build -> Settings) as above select the GCC C++ Linker option and from there go to Miscellaneous(second to last option). Add the following Linker flags(the field was empty for me): -Wl,--no-as-needed -pthread. Hit apply.

3)添加一个宏.再次从项目属性菜单(项目->右键单击->属性).导航至C/C++ General -> Paths and symbols -> Symbols.选择GNU C++.添加名称为__GXX_EXPERIMENTAL_CXX0X__且没有值的符号.再次点击应用.

3) Add a macro. Again from the project properties menu(project->right click->properties). Navigate to C/C++ General -> Paths and symbols -> Symbols. Select GNU C++. Add a symbol with the name __GXX_EXPERIMENTAL_CXX0X__ and no value. Again hit apply.

4)导航到C/C++ General -> Preprocessor Include paths..选择提供程序选项卡.在此选项卡中,仅选中以下两个选项:CDT GCC Built-in Compiler SettingsCDT Managed Build Setting Entries.选择CDT GCC Built-in Compiler Settings取消选中Share setting entries between projects(global provider)复选框,现在应启用标有Command to get compiler specs的文本框.在此文本框中附加旧的-std=c++11.对于我${COMMAND} -E -P -v -dD ${INPUTS} -std=c++11,文本现在看起来像这样.最后一次点击应用.

4) Navigate to C/C++ General -> Preprocessor Include paths.. Select the providers tab. In this tab leave only the following two options checked: CDT GCC Built-in Compiler Settings and CDT Managed Build Setting Entries. Select CDT GCC Built-in Compiler Settings uncheck the checkbox Share setting entries between projects(global provider) and now the text box labeled Command to get compiler specs should be enabled. In this text box append the good old -std=c++11. The text now looks like this for me ${COMMAND} -E -P -v -dD ${INPUTS} -std=c++11. Hit apply one last time.

5)重建项目的索引.为此,right click the project->Index->Rebuild

5) Rebuild the index for the project. To do that right click the project->Index->Rebuild

按照这些步骤操作,我能够编译一个c ++ 11多线程程序,执行该程序,并且Eclipse CDT也没有报告任何错误,并且对自动完成很有帮助.不幸的是,此设置必须针对Release和Debug分别进行(或者至少我还没有找到共享它的方法).希望这可以帮助.

Following these steps I was able to compile a c++11 multithreaded program, execute it and also Eclipse CDT did not report any errors and was helpful with the autocompletion. Unfortunately this setting has to be done separately for Release and Debug(or at least I have not found a way to share it). Hope this helps.

这篇关于Eclipse和c ++ 11中的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆