【发布时间】:2021-01-22 12:41:47
【问题描述】:
所以我一直在尝试使用 MinGW 编译器让以下代码在 Windows 上编译和运行。
#include <iostream>
#include <thread>
#include <future>
void ThreadWork(std::promise<int> &&p)
{
// Pre-return from thread, do work after returning a value
/* if else and things */ p.set_value(0);
std::cout << "From thread" << std::endl;
// More work
}
int main()
{
std::promise<int> p;
std::future<int> f = p.get_future();
std::thread th(ThreadWork, std::move(p));
// Get return value from thread
int ret = f.get();
}
上面给出了错误:
error: aggregate 'std::promise<int> p' has incomplete type and cannot be defined
75 | std::promise<int> p;
error: variable 'std::future<int> f' has initializer but incomplete type
76 | std::future<int> f = p.get_future();
error: 'thread' is not a member of 'std'
77 | std::thread th(readInThread, callback, std::move(p));
尝试使用官方 MinGW 构建,尝试使用 meganz/mingw-std-threads(即使设置为 0x0A00,似乎也未设置 _WIN32_WININT 之类的错误,如果我尝试手动设置编译失败并重新声明),还有很多更多,没有成功...
我还尝试与 pthread 链接:-lpthread 不走运 :(
为什么 C++11 标准功能仍未在 MinGW 中实现?他们目前的替代方案是什么?
【问题讨论】:
-
@TedLyngmo 对不起,我确实添加了,但仍然没有运气,同样的错误。
-
要链接
-lpthread,请确保已安装。如果您使用的是 cmake,请先尝试find_package(Threads REQUIRED) -
你用的是什么编译器? g++ 7.3.0 编译这个没有错误。
-
@TonyK g++ 9.2.0 @UmarFarooq ????
std::命名空间中的符号不包含在 stdlib 中吗? -
代码中还有一些其他问题可能会分散人们对实际问题的注意力。 demo - 此外,如果您需要与
pthread链接,请使用-pthread选项,而不是-lpthread链接指令。
标签: c++ windows multithreading c++11 mingw