我做了功课,并在这里和网上寻找答案。下面的简单代码没有编译:
#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。