我正在尝试编译一个简单的 c++ 程序,该程序在 eclipse kepler/mingw 4.8.1 和 win32 上使用 std::thread。我希望在 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(
^
有人可以建议我可能缺少什么来正确编译吗?
最佳答案
普通 MinGW 不支持 std::thread
.您将需要使用启用了“posix”线程的 MinGW-w64 工具链(例如 Qt 5 附带的工具链),以便 libstdc++ 公开 <thread>
, <mutex>
和 <future>
功能。
您可以找到安装程序 here ,但您也可以尝试只替换整个 mingw
带有 one of these packages 的工具链根文件夹.可以选择32位或64位,记得选择threads-posix
如果你想玩std::thread
和 friend 。除了您已经拥有的那些之外,不需要特殊的编译器选项。我建议使用 -std=c++11
如果您不需要 GCC 4.6 兼容性。
关于c++ - std::thread 不是使用 Eclipse Kepler MinGW 命名空间 std 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62508669/