2010-03-25 102 views
15

我编译&使用macports安装了gcc4.4。std ::线程错误(线程不是std的成员)

当我尝试使用编译 - >克++ -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 <<...编译好..

任何人都可以帮我吗?

+0

如果您在螺纹头看,它似乎该类只存在'#如果定义(_GLIBCXX_HAS_GTHREADS)&&定义(_GLIBCXX_USE_C99_STDINT_TR1)' 。我不确定,但是你必须做些什么才能定义这些定义。 – UncleBens 2010-03-25 22:46:30

+0

@UncleBens:我相信这些是直接由-pthread和-std = C++ 0x定义的。省略-pthread导致段错误:http://gcc.gnu.org/ml/gcc-help/2009-04/msg00208.html – 2010-03-26 03:47:26

+0

只是最新的更新:MacPorts gcc 4.7.0支持/编译std :: thread,而4.6.3没有。 – 2012-04-26 09:45:59

回答

5

丢弃-ansi,这意味着-std = C++ 98,你显然不想要。这也导致宏__STRICT_ANSI__被定义,并且这可能改变标题的行为,例如,通过禁用C++ 0x支持。

+0

宏名实际上在末尾有两个下划线,但是如果我正确写入,会误将它解释为格式。 – Tronic 2010-03-25 21:54:46

+0

指出,我删除它,但它仍然给我同样的错误..我用gcc4.4和gcc4.5测试版...这是令人沮丧的。 – luis 2010-03-25 21:55:01

+0

修复了缺少的下划线问题。 – 2010-03-26 14:23:48

13

GCC不完全支持的std ::线程尚未:

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; 
} 

输出 a.out
 
g++ -Wall -g -std=c++0x -pthread main.cpp 

编译:

 
Printing from another thread 

你能提供完整的代码吗?也许潜伏在这些...中有一些隐晦的问题?

+0

我试过你的代码,并且得到相同的错误... 这可能与OSX有关吗?或者,也许是由MacPorts安装的GCC不正确的东西? – luis 2010-03-26 09:54:53

+1

这可能是MacPorts不完全支持C++ 0x功能?你有gcc configure脚本的输出吗?这里有一个Mac用户,其配置指定std :: thread不受支持:(http://www.mail-archive.com/[email protected]/msg00973.html) – 2010-03-26 16:44:54

0

嗯,我尝试在Ubuntu上使用GCC 4.4.1,它的作用就像一个魅力。 问题是Mac OS X的具体情况,现在只需要找出原因...

1

我在使用MinGW的窗口上遇到了同样的问题。我发现在github上包装类mingw-std-threads包括 mingw.mutex.h,mingw.thread.h文件到全局MinGW目录解决了这个问题。所有我必须做的是包含头文件和我的代码保持不变

#include "mingw.thread.h" 

... 
std::thread t(handle); 
... 
+0

为此欢呼。正在让我发疯。你提到全球MinGW目录时,你的回答有点混乱。我不得不将文件放在其他所有.h文件所需的项目文件夹中,但它工作正常。 – CoreyRS 2018-01-13 10:20:24