softmachine 发表于 2013-04-09 22:56

'std::this_thread' has not been declared

本帖最后由 softmachine 于 2013-04-09 23:08 编辑

操作系统:XP
IDE工具:code::blocks 12.11
编译器版本:mingw32-g++ 4.7.1
编译参数:mingw32-g++.exe -std=c++11 -std=c++0x -pthread -D_GLIBCXX_USE_NANOSLEEP -D_GLIBCXX_USE_SCHED_YIELD-g   -c D:\CodeBlocksProjects\MyServer\MyServer\main.cpp -o obj\Debug\main.o

报错信息如下:

D:\CodeBlocksProjects\MyServer\MyServer\main.cpp: In function 'void foo()':
D:\CodeBlocksProjects\MyServer\MyServer\main.cpp:11:10: error: 'std::this_thread' has not been declared
D:\CodeBlocksProjects\MyServer\MyServer\main.cpp: In function 'int main()':
D:\CodeBlocksProjects\MyServer\MyServer\main.cpp:22:5: error: 'thread' is not a member of 'std'
D:\CodeBlocksProjects\MyServer\MyServer\main.cpp:22:17: error: expected ';' before 't'
Process terminated with status 1 (0 minutes, 0 seconds)
3 errors, 0 warnings (0 minutes, 0 seconds)


=================================================================
编译的代码为:

#include <iostream>
#include <thread>
#include <chrono>


void foo()
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
}


int main()
{
    std::thread t(foo);
    t.join();
    return 0;
}


该怎么解决以上的编译报错问题,-std=c++11 和相关的宏参数都已加上了问题依旧,难道是版本不支持??

linux_c_py_php 发表于 2013-04-09 23:17

用c++11, 向脑残进发。

cascle 发表于 2013-04-09 23:25

答案应该在这里吧http://stackoverflow.com/questions/12523122/what-is-glibcxx-use-nanosleep-all-about,_GLIBCXX_USE_NANOSLEEP这个宏没定义#ifdef _GLIBCXX_USE_NANOSLEEP
...
/// sleep_for
template<typename _Rep, typename _Period>
inline void
sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
...

cascle 发表于 2013-04-09 23:30

不过比较奇怪的是编译参数里边已经加入-D_GLIBCXX_USE_NANOSLEEP了{:3_182:}

bruceteen 发表于 2013-04-10 08:36

我用mingw4.7.2搞过,没能搞起来,缺太多本地的实现

而对于你的这个编译错误,只要打开<thread>看一下就明白了,比如我我看到
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
所以,只要在我的代码#include <thread>前加上
#define _GLIBCXX_HAS_GTHREADS
#define _GLIBCXX_USE_C99_STDINT_TR1
就行

softmachine 发表于 2013-04-10 08:45

bruceteen 发表于 2013-04-10 08:36 static/image/common/back.gif
我用mingw4.7.2搞过,没能搞起来,缺太多本地的实现

而对于你的这个编译错误,只要打开看一下就明白了, ...


这两个宏我也试过,这样的结果是报18个错误,都来自thread头文件中。

softmachine 发表于 2013-04-10 08:55

bruceteen 发表于 2013-04-10 08:36 static/image/common/back.gif
我用mingw4.7.2搞过,没能搞起来,缺太多本地的实现

而对于你的这个编译错误,只要打开<thread>看一下就明白了,比如我我看到
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
所以,只要在我的代码#include <thread>前加上
#define _GLIBCXX_HAS_GTHREADS
#define _GLIBCXX_USE_C99_STDINT_TR1
就行


加上以上两个宏定义后,会报thread头文件中一些变量或类型没有定义,诸如:
d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\thread|63|error: '__gthread_t' does not name a type|
之类的。。。。。。。

kandia 发表于 2013-04-10 09:31

升级到4.8应该就没这个问题了。gcc4.8修正了sleep_for的一些宏依赖

softmachine 发表于 2013-04-15 09:03

mingw的GCC4.7版本貌似c++11的thread有点问题,已经转向boost::thread。boost::thread运行良好。

bruceteen 发表于 2013-07-17 16:30

在 http://sourceforge.net/projects/mingwbuilds/ 下载 mingw-builds-install.exe
安装时,第二步的线程模式选择 posix,千万不能选 windows

我安装的是最新的 4.8.1,你的代码顺利编译运行通过
页: [1] 2
查看完整版本: 'std::this_thread' has not been declared