Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

c++ - std::thread is not a member of namespace std using Eclipse Kepler MinGW

I'm trying to compile a simple c++ program that uses std::thread on eclipse kepler / mingw 4.8.1 and win32. I hope to move development to linux at some point after many years on windows development.

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


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

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

    Test::DestroyInstance();
    return 0;
}

Ignoring the purpose of test (it's a singleton that just produces some output that I will expand upon, once I get the std::thread working!)

The g++ compiler settings I've set in Eclipse are:

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

And the preprocessor symbol I have defined is:

__GXX_EXPERIMENTAL_CXX0X__

Building complains that std::thread is not a member of 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(
  ^

Can someone suggest what I might be missing to get this compiling correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Plain MinGW cannot support std::thread. You will need to use a MinGW-w64 toolchain (such as those shipped with Qt 5) that has "posix" threading enabled, so that libstdc++ exposes the <thread>, <mutex> and <future> functionality.

You can find an installer here, but you can also try just replacing the whole mingw toolchain root folder with one of these packages. You can choose 32- or 64-bit, remember to select threads-posix if you want to play with std::thread and friends. No special compiler options other than the ones you already have are needed. I do suggest using -std=c++11 if you don't need GCC 4.6 compatibility.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...