【发布时间】:2013-04-26 02:03:57
【问题描述】:
我做了功课,并在这里和网上寻找答案。下面的简单代码没有编译:
#include <thread>
#include <iostream>
void hello()
{
std::cout << "Hello from thread " << std::endl;
}
int main()
{
std::thread t1(hello);
t1.join();
return 0;
}
这是非常简单的代码,但我收到以下错误:
Thread_Cpp11_002.cpp: In function 'int main()'
Thread_Cpp11_002.cpp:14:5: error: 'thread' is not a member of 'std'
Thread_Cpp11_002.cpp:14:17: error: expected ';' before 't1'
Thread_Cpp11_002.cpp:15:5: error: 't1' was not declared in this scope
我已经尝试了很多东西,但都没有奏效:
- 声明
t1使用thread而不是std::thread - 使用 g++ 4.8.0 编译
- 单独和一起使用以下标志:
-pthread、-std=gnu++11、-std=c++0x、-std=c++11...
所以,我认为这个问题值得在这里发布。
这些编译是通过命令行完成的。让它工作后,我将尝试使用 Code::Blocks 12.11,它在 C++98 上运行良好。
记住,我使用的是 Windows Vista。
【问题讨论】:
-
没有关于缺少
#include的错误? -
你传递给编译器的参数是什么?它应该有 -std=c++11 。
-
@zneak,我也尝试使用“thread.h”或
进行更改,在这两种情况下,编译都没有启动(没有这样的文件)。 -
@Elazar,是的,我通过了:g++ -std=c++11 -o ...
-
@ajmacedo 和
-pthread?
标签: c++ multithreading c++11 windows-vista mingw32