首页 文章

std :: thread不是使用Eclipse Kepler MinGW的命名空间std的成员

提问于
浏览
1

我正在尝试编译一个在eclipse kepler / mingw 4.8.1和win32上使用std :: thread的简单c程序 . 我希望在Windows开发多年后将开发转移到linux上 .

#include "test.h"
#include <thread>
#include <algorithm>


int main()
{
    Test::CreateInstance();

    std::thread(
        [&]()
        {
            Test::I()->Output2();
        }
    );

    Test::DestroyInstance();
    return 0;
}

忽略测试的目的(一旦我得到std :: thread工作,它就是一个只产生一些我会扩展的输出的单例!)

我在Eclipse中设置的g编译器设置是:

-c -fmessage-length=0  -std=c++0x -Wc++0x-compat

我定义的预处理器符号是:

__GXX_EXPERIMENTAL_CXX0X__

构建抱怨std :: thread不是std的成员:

10:30:13 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -Wc++0x-compat -o main.o "..\\main.cpp" 
..\main.cpp: In function 'int main()':
..\main.cpp:11:2: error: 'thread' is not a member of 'std'
  std::thread(
  ^

有人可以建议我可能缺少什么来正确编译?

3 回答

  • 0

    普通MinGW不支持 std::thread . 您将需要使用启用了"posix"线程的MinGW-w64工具链(例如Qt 5附带的工具链),以便libstdc公开 <thread><mutex><future> 功能 .

    您可以找到安装程序here,但您也可以尝试用one of these packages替换整个 mingw 工具链根文件夹 . 您可以选择32位或64位,如果您想和 std::thread 和朋友一起玩,请记得选择 threads-posix . 除了您已经拥有的选项之外,不需要特殊的编译器选项 . 如果您不需要GCC 4.6兼容性,我建议使用 -std=c++11 .

  • 3

    虽然我使用的是Cygwin编译器,但我遇到了同样的问题 . 我所做的是为预处理器定义符号 __cplusplus ,其值为 201103L . 我还使用了标志 -std=c++11 作为编译器 . 必须对所有配置(调试和释放)进行此设置 .

    您可以检查的另一件事是您已正确安装编译器,验证您的编译器安装,如rubenvb所述 .

  • 0

    请参阅此处了解可添加到MinGW的任何C 11版本的本机实现:https://github.com/meganz/mingw-std-threads这是一个仅限标头的库,因此您只需要在项目中包含标头,您将获得C 11线程和同步原语 .

相关问题