【问题标题】:MinGw 4.7.0 doesnt compile code with #include <boost/thread>MinGw 4.7.0 不使用#include <boost/thread> 编译代码
【发布时间】:2011-11-24 07:57:01
【问题描述】:

MinGw 4.7.0 提升 1.47
代码:

#include <iostream>
#include <boost/thread.hpp>
int main(int argc, char *argv[])
{
std::cout<<"In main"<<std::endl;
}

编译器字符串:

g++.exe -Wall -fexceptions  -g    -IC:\soft\ides_comp\mingw\include\boost_1_47_0  -c      D:\work\cpp_cb\mt1\main.cpp -o obj\Debug\main.o

无论我尝试什么编译器选项,它都无法编译,并且出现相同的错误。

警告:
In file included from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_data.hpp:12:0, from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread.hpp:15, from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread.hpp:13, from D:\work\cpp_cb\mt1\main.cpp:2: C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:59:40: warning: inline function 'void* boost::detail::allocate_raw_heap_memory(unsigned int)' declared as dllimport: attribute ignored [-Wattributes] C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:69:39: warning: inline function 'void boost::detail::free_raw_heap_memory(void*)' declared as dllimport: attribute ignored [-Wattributes]

错误:
In file included from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/shared_ptr.hpp:30:0, from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/shared_ptr.hpp:17, from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/date_time/time_clock.hpp:17, from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread_time.hpp:9, from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_data.hpp:10, from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread.hpp:15, from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread.hpp:13, from D:\work\cpp_cb\mt1\main.cpp:2:

C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/checked_delete.hpp: In instantiation of 'void boost::checked_delete(T*) [with T = boost::error_info&lt;boost::tag_original_exception_type, const std::type_info*&gt;]':


C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/detail/shared_count.hpp:95:13: required from 'boost::detail::shared_count::shared_count(Y*) [with Y = boost::error_info<boost::tag_original_exception_type, const std::type_info*>]' C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/shared_ptr.hpp:183:50: required from 'boost::shared_ptr<T>::shared_ptr(Y*) [with Y = boost::error_info<boost::tag_original_exception_type, const std::type_info*>; T = boost::error_info<boost::tag_original_exception_type, const std::type_info*>]' C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/info.hpp:171:69: required from 'const E& boost::exception_detail::set_info(const E&, const boost::error_info<Tag, T>&) [with E = boost::unknown_exception; Tag = boost::tag_original_exception_type; T = const std::type_info*]' C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/info.hpp:192:46: required from 'typename boost::enable_if<boost::exception_detail::derives_boost_exception<E>, const E&>::type boost::operator<<(const E&, const boost::error_info<Tag, T>&) [with E = boost::unknown_exception; Tag = boost::tag_original_exception_type; T = const std::type_info*; typename boost::enable_if<boost::exception_detail::derives_boost_exception<E>, const E&>::type = const boost::unknown_exception&]' C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/detail/exception_ptr.hpp:182:13: required from 'void boost::unknown_exception::add_original_type(const E&) [with E = std::exception]' C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/detail/exception_ptr.hpp:161:32: required from here

警告 2:
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/checked_delete.hpp:34:5: warning: deleting object of polymorphic class type 'boost::error_info&lt;boost::tag_original_exception_type, const std::type_info*&gt;' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]

可能是什么?谢谢

【问题讨论】:

  • 什么是 MinGW 4.7.0?你是说 MinGW GCC 4.7 吗?
  • 我从这里猜测:http://code.google.com/p/mingw-builds/。试试4.6.2看看问题是否依旧。

标签: c++ boost mingw boost-thread


【解决方案1】:

这已在以后的 boost 版本中得到修复。在这里检查:

https://svn.boost.org/trac/boost/ticket/6165

更新 boost/config/stdlib/libstdcpp3.hpp:

#if defined(_GLIBCXX_HAVE_GTHR_DEFAULT) \
    || defined(_GLIBCXX_HAS_GTHREADS) \
    || defined(_GLIBCXX__PTHREADS) \
    || defined(WIN32)

添加了 _GLIBCXX_HAS_GTHREADS 和/或 WIN32(来自上面的链接,请参阅补丁)。

WIN32 似乎有点蛮力,但没有它就无法工作(至少对我来说不是。我使用的是较旧的 bo​​ost 版本 1.42)。

【讨论】:

    【解决方案2】:

    如果您编译线程库以使用 Win32 线程,您可能需要

    -DBOOST_USE_WINDOWS_H
    

    在您的编译器标志中。虽然我认为这不是问题(我会因为不使用它而收到一组不同的错误),但您可以尝试一下。

    【讨论】:

      【解决方案3】:

      这个答案可能不是很直接,但是: 我确实使用 g++ 4.7.0 20111209,它支持线程。

      #include <iostream>
      #include <thread>
      
      using namespace std;
      
      void foo()
      {
          cout << "Hello from thread\n";
      }
      
      int main()
      {
          thread th1(foo);
          th1.join();
          return 0;
      }
      

      在编译期间使用 -std=c++11 选项来使用支持线程的最新标准

      PS: 在我的电脑上使用 mingw 4.7.0 编译的 boost 1.48 会出现错误(尝试使用 boost::thread 时): 线程支持不可用:它已被 BOOST_DISABLE_THREADS 明确禁用 所以我猜它默认在 g++ 4.7.0 中被禁用(在 g++ 4.6.2 中它不是)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-17
        • 2021-07-06
        • 2015-12-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多