如何编译代码使用#include< thread> [英] How to compile the code using #include <thread>

查看:1353
本文介绍了如何编译代码使用#include< thread>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译一些使用线程的C ++代码:

I am trying to compile some C++ code that uses threads:

#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 到命令行,如错误消息中所述。

<thread> and standard threading support is a new feature (defined in the C++11 standard). As for g++, you have to enable it adding -std=c++0x to the command line, as explained in the error message.

此外,您使用的是非标准(Microsoft特定)主,使用经典和正常 / code>:

Also, you are using a nonstandard (Microsoft-specific) main, use the "classic" main and normal 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 ++而言,您可以在此处找到其实现的状态。

Notice that not all C++11 features are available in current compilers; as far as g++ is concerned, you can find the status of their implementation here.

这篇关于如何编译代码使用#include&lt; thread&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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