【问题标题】:c++11 std::async doesn't work in mingwc++11 std::async 在 mingw 中不起作用
【发布时间】:2012-04-18 12:58:58
【问题描述】:

从 Herb Sutter 的 presentation 运行此代码。这在 gcc 4.6.3 下的 linux 中运行良好。我在想mingw不支持future.h,但是这个错误真的很难理解!

#include <iostream>
#include <vector>
#include <string>
#include <future>
#include <algorithm>

using namespace std;

string flip( string s ) {
  reverse( begin(s), end(s) );
  return s;
}

int main() {
  vector<future<string>> v;

  v.push_back( async([]{ return flip(" ,olleH"); }) );
  v.push_back( async([]{ return flip(".gnaL"); }) );
  v.push_back( async([]{ return flip("\n!TXEN"); }) );

  for( auto& e: v ) {
    cout << e.get();
  }
}

这里是错误:

$ x86_64-w64-mingw32-g++.exe -std=c++0x -pthread swap.cpp
swap.cpp: In function 'int main()':
swap.cpp:17:51: error: invalid use of incomplete type 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
swap.cpp:18:49: error: invalid use of incomplete type 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
swap.cpp:19:51: error: invalid use of incomplete type 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
swap.cpp:22:14: error: invalid use of incomplete type 'class std::future<std::basic_string<char> >'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of 'class std::future<std::basic_string<char> >'
In file included from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_algobase.h:68:0,
                 from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/char_traits.h:41,
                 from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/ios:41,
                 from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/ostream:40,
                 from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/iostream:40,
                 from swap.cpp:1:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_iterator.h: In instantiation of '__gnu_cxx::__normal_iterator<_Iterator, _Container>& __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator++() [with _Iterator = std::future<std::basic_string<char> >*; _Container = std::vector<std::future<std::basic_string<char> > >; __gnu_cxx::__normal_iterator<_Iterator, _Container> = __gnu_cxx::__normal_iterator<std::future<std::basic_string<char> >*, std::vector<std::future<std::basic_string<char> > > >]':
swap.cpp:21:17:   required from here
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_iterator.h:750:2: error: cannot increment a pointer to incomplete type 'std::future<std::basic_string<char> >'
In file included from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/vector:65:0,
                 from swap.cpp:2:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = std::future<std::basic_string<char> >; _Alloc = std::allocator<std::future<std::basic_string<char> > >]':
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_vector.h:247:15:   required from 'std::vector<_Tp, _Alloc>::vector() [with _Tp = std::future<std::basic_string<char> >; _Alloc = std::allocator<std::future<std::basic_string<char> > >]'
swap.cpp:15:26:   required from here
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_vector.h:161:9: error: invalid use of incomplete type 'class std::future<std::basic_string<char> >'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of 'class std::future<std::basic_string<char> >'
In file included from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/vector:63:0,
                 from swap.cpp:2:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_construct.h: In instantiation of 'void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::future<std::basic_string<char> >*]':
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_construct.h:155:7:   required from 'void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator<_T2>&) [with _ForwardIterator = std::future<std::basic_string<char> >*; _Tp = std::future<std::basic_string<char> >]'
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_vector.h:403:9:   required from 'std::vector<_Tp, _Alloc>::~vector() [with _Tp = std::future<std::basic_string<char> >; _Alloc = std::allocator<std::future<std::basic_string<char> > >]'
swap.cpp:15:26:   required from here
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_construct.h:128:7: error: invalid use of incomplete type '_Value_type {aka class std::future<std::basic_string<char> >}'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of '_Value_type {aka class std::future<std::basic_string<char> >}'
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future: At global scope:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:187:5: error: 'typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type std::async(_Fn&&, _Args&& ...) [with _Fn = main()::<lambda()>; _Args = {}; typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type = std::future<std::basic_string<char> >]', declared using local type 'main()::<lambda()>', is used but never defined [-fpermissive]
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:187:5: error: 'typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type std::async(_Fn&&, _Args&& ...) [with _Fn = main()::<lambda()>; _Args = {}; typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type = std::future<std::basic_string<char> >]', declared using local type 'main()::<lambda()>', is used but never defined [-fpermissive]
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:187:5: error: 'typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type std::async(_Fn&&, _Args&& ...) [with _Fn = main()::<lambda()>; _Args = {}; typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type = std::future<std::basic_string<char> >]', declared using local type 'main()::<lambda()>', is used but never defined [-fpermissive]

我在 Windows 中使用 GCC 4.7。

$ g++ -v
Using built-in specs.
COLLECT_GCC=c:\mingw64\bin\x86_64-w64-mingw32-g++.exe
COLLECT_LTO_WRAPPER=c:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/4.7.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-4.7.0/configure --build=x86_64-w64-mingw32 --enable-targets=all --disable-multilib --enable-64bit --prefix=/mingw64 --with-sysroot=/mingw64 --disable-shared --enable-static --disable-nls --enable-version-specific-runtime-libs --disable-win32-registry --without-dwarf2 --enable-sjlj-exceptions --enable-fully-dynamic-string --enable-languages=c,ada,lto,c++,objc,obj-c++,fortran --enable-libgomp --enable-lto --enable-libssp -enable-gnattools --disable-bootstrap --with-gcc --with-gnu-as --with-gnu-ld --with-stabs --enable-interwork --with-mpfr-include=/home/beta/gcc-build/../gcc-4.7.0/mpfr/src --with-mpfr-lib=/home/beta/gcc-build/mpfr/src/.libs
Thread model: win32
gcc version 4.7.0 (GCC)

【问题讨论】:

  • 如果您的 GCC 是 4.6.3,它不应该选择 4.7 模板库。
  • mingw中的编译器版本是多少?
  • 我在 Windows 中使用 4.7。用 gcc -v 编辑问题。谢谢。
  • 适用于我在 Mac 上的 GCC 4.7.0。
  • 对。我的意思是 Windows 平台上的 GCC。 VS11 beta 支持这个程序。

标签: c++ c++11 concurrency mingw stdasync


【解决方案1】:

也许我更容易解释,因为我写了那段代码,但也没那么难,只要看看错误输出的第一行:

swap.cpp:22:14: error: invalid use of incomplete type 'class std::future<std::basic_string<char> >'

这告诉您std::future 是一个不完整的类型,即它已声明但未定义。下一行告诉你它的确切声明位置(然后所有其他错误都是由于尝试以不同方式使用该不完整类型而引起的。)

如果您查看 GCC 的 &lt;future&gt; 标头,您会看到类型在顶部附近声明,但定义取决于此预处理器条件:

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
  && (ATOMIC_INT_LOCK_FREE > 1)

如果你用这些宏编译一些简单的测试,你会发现_GLIBCXX_HAS_GTHREADS 没有定义,那是因为你的gcc -v 显示了

线程型号:win32

目前还没有人提供使 C++11 线程功能在 Windows 上运行所需的代码。

&lt;future&gt; 工作会更难,但实际上启用&lt;thread&gt;&lt;mutex&gt; 并没有那么难,但还没有人加紧工作。昨天我发了一些关于如何为win32线程模型启用C++11线程特性的想法:http://gcc.gnu.org/ml/libstdc++/2012-05/msg00020.html

【讨论】:

  • 这在 2015 年仍然不起作用,再次出现神秘错误。 std::async 是否可以在 MinGW-w64 的任何其他线程模型风格下工作? (如果是这样,那会有点令人费解,因为所有模型都必须在某个时候归结为 Windows API 线程调用——而且还有有用的信息,因为我可以得到另一种风格)
【解决方案2】:

名为“MinGW builds”的特殊 MinGW 构建确实通过 gcc 提供 C++11 支持,包括 asyncthreadfuture 和朋友

(这秒是 IcemanX 否决的答案)

已移至http://sourceforge.net/projects/mingwbuilds/

正如 Jonathan Wakely(正确而权威地)指出的那样,其他版本的 GCC 的问题是它们不提供对 pthreads 的支持,但 MinGW-builds 提供。使用 Windows 线程的解决方案可能会更好,但目前不可用。

我写了详细的MinGW build+eclipse的安装设置说明,这里:http://scrupulousabstractions.tumblr.com/post/36441490955/eclipse-mingw-builds

【讨论】:

  • MinGW-builds 不是 MinGW 的正式版本:也许你想澄清一下。你的标题可能会让人误解。但信息很有用。谢谢。
【解决方案3】:

不知道 std::async 是否有有效的实现(如果有的话),但 std::thread 可用于 MinGW/GCC 4.7 版本,可在项目站点 http://code.google.com/p/mingw-builds/ 获得

希望对你有帮助

【讨论】:

  • 是的,我真的不想在这里实现任何特定的东西;只是为了测试 mingw 中的 c++11 功能。不过,谢谢。
【解决方案4】:

标准 MinGW 库和编译器不支持std::async(*args)

但是有一个修改版的MinGW,和原来的功能一样,就是:TDM-GCC MinGW

它可以免费下载并定期更新。此外,它还支持 32 位和 64 位 用于编译代码。

希望对你有帮助^^

【讨论】:

    【解决方案5】:

    只需从here 下载 MinGW 安装程序并使用配置 posix 线程进行安装。

    如果您已经安装了旧的 MinGW,请编辑您的环境变量,以便使用新安装的 MinGW。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多