gpt4 book ai didi

c++ - 如何使用#include 编译代码

转载 作者:可可西里 更新时间:2023-11-01 18:19:08 38 4
gpt4 key购买 nike

我正在尝试编译一些使用线程的 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 .

关于c++ - 如何使用#include <thread> 编译代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8513980/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com