XCode std::thread C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14011955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XCode std::thread C++
提问by Matthijn
For a small project for school I need to create a simple client / server construction which will run on a router (with openWRT) and I am trying to do something with threads in this application.
对于学校的一个小项目,我需要创建一个简单的客户端/服务器结构,它将在路由器(使用 openWRT)上运行,并且我正在尝试使用此应用程序中的线程做一些事情。
My C++ skills are very limited, so I found this on the internetas an basic example.
我的 C++ 技能非常有限,所以我在互联网上找到了这个作为基本示例。
#include <thread>
#include <iostream>
void doSomeWork( void )
{
std::cout << "hello from thread..." << std::endl;
return;
}
int main( int argc, char *argv[] )
{
std::thread t( doSomeWork );
t.join();
return 0;
}
When I try to run this in Xcode (4.5.2) I get the following error:
当我尝试在 Xcode (4.5.2) 中运行它时,出现以下错误:
Attempt to use an deleted function
尝试使用已删除的函数
And it shows some code of:
它显示了一些代码:
__threaad_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
{
__invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
}
I think I need to do something with 'build settings' or 'link library' or something? But I am not quite sure what to do exactly. I thought I might need to set the following settings (which i found here)
我想我需要用“构建设置”或“链接库”之类的东西做些什么?但我不太确定到底该怎么做。我想我可能需要设置以下设置(我在这里找到的)
- In the Build Settings tab for your project, scroll down to "Apple LLVM Compiler 4.1 - Language"
- Set the setting "C++ Language Dialect" to "C++11 [-std=c++11]"
- Set the setting "C++ Standard Library" to "libc++ (LLVM standard C++ library with C++11 support)"
- 在项目的 Build Settings 选项卡中,向下滚动到“Apple LLVM Compiler 4.1 - Language”
- 将设置“C++ 语言方言”设置为“C++11 [-std=c++11]”
- 将设置“C++ 标准库”设置为“libc++(支持 C++11 的 LLVM 标准 C++ 库)”
But those settings where already set.
但是那些设置已经设置好了。
Is there any flag / library or something I am missing?
是否有任何标志/图书馆或我缺少的东西?
回答by bartimar
Use G++ instead of LLVM in XCode. Don't forget to link thread libs (-lpthread - or -pthread, -lrt) in compiler build settings. And count with the differences of thread behaviour across Win/Mac/Linux OS (despite it's POSIX)
在 XCode 中使用 G++ 而不是 LLVM。不要忘记在编译器构建设置中链接线程库(-lpthread - 或 -pthread,-lrt)。并计算 Win/Mac/Linux 操作系统中线程行为的差异(尽管它是 POSIX)