我正在尝试编译一些使用线程的 C++ 代码:
#include <iostream>
#include <thread>
void hello()
{
std::cout<<"Hello Concurrent World\n";
}
int _main(int argc, _TCHAR* argv[])
{
std::thread t(hello);
t.join();
return 0;
}
编译时出现错误:
c:\temp\app1\app1\app1.cpp(6): fatal error C1083: Cannot open
include file: 'thread': No such file or directory
~/Documents/C++ $ g++ -o thread1 thread1.cpp -D_REENTRANT -lpthread
In file included from /usr/include/c++/4.5/thread:35:0,
from thread1.cpp:2:
/usr/include/c++/4.5/bits/c++0x_warning.h:31:2: error: #error This file
requires compiler and library support for the upcoming ISO C++ standard,
C++0x. This support is currently experimental, and must be enabled with
the -std=c++0x or -std=gnu++0x compiler options.
我该如何修复这些错误?
<thread>
标准线程支持是一项新功能(在 C++11 标准中定义)。至于g++,你必须启用它添加-std=c++0x
到命令行,如错误消息中所述。
另外,您使用的是非标准(Microsoft 特定的)主,请使用“经典”main
和正常的char
:
// thread1.cpp
#include <iostream>
#include <thread>
void hello()
{
std::cout<<"Hello Concurrent World\n";
}
int main(int argc, char * argv[])
{
std::thread t(hello);
t.join();
return 0;
}
请注意,并非所有 C++11 功能在当前编译器中都可用;就g++而言,你可以找到它们的实现状态here.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)