我使用macports编译并安装了gcc4.4。
当我尝试使用 -> g++ -g -Wall -ansi -pthread -std=c++0x main.cpp... 进行编译时:
#include <thread>
...
std::thread t(handle);
t.join();
....
编译器返回:
cserver.cpp: In member function 'int CServer::run()':
cserver.cpp:48: error: 'thread' is not a member of 'std'
cserver.cpp:48: error: expected ';' before 't'
cserver.cpp:49: error: 't' was not declared in this scope
但是
std::cout <<...
编译得很好..
有人可以帮助我吗?
gcc 尚未完全支持 std::thread:
http://gcc.gnu.org/projects/cxx0x.html
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
同时使用 boost::thread。
编辑
尽管以下内容在 gcc 4.4.3 上编译并运行良好:
#include <thread>
#include <iostream>
struct F
{
void operator() () const
{
std::cout<<"Printing from another thread"<<std::endl;
}
};
int main()
{
F f;
std::thread t(f);
t.join();
return 0;
}
编译
g++ -Wall -g -std=c++0x -pthread main.cpp
a.out
的输出:
从另一个线程打印
可以提供完整的代码吗?也许那些
...
中潜藏着一些不为人知的问题?
我在使用 MinGW 的 Windows 上遇到了同样的问题。我在 github mingw-std-threads 上找到了 in 的包装类,包括 mingw.mutex.h、mingw.thread.h 文件到全局 MinGW 目录修复了此问题。我所要做的就是包含头文件,并且我的代码保持不变
#include "mingw.thread.h"
...
std::thread t(handle);
...
删除-ansi,这意味着-std=c++98,这显然是你不想要的。它还会导致宏
__STRICT_ANSI__
被定义,这可能会改变标头的行为,例如通过禁用 C++0x 支持。
使用 MSYS2 将您的 MinGW 编译器更新到最新版本以解决此错误,或尝试使用 MSYS2 重新安装 MinGW
cserver.cpp:48: error: 'thread' is not a member of 'std'