如果在 linux Ubuntu 中包含 boost/thread,则不会编译:)10.10

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5389853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:21:54  来源:igfitidea点击:

doesn't compile if included boost/thread in linux Ubuntu:)10.10

c++linuxboostcompiler-errors

提问by andrissh

I work in eclipse under linux UBUNTU:) 10.10, installed the boost-dev packages 1.40 using the Synaptic pkg manager. I am new to linux and this boost pkg. I try to create a new project, and write:

我在 linux UBUNTU 下的 eclipse 中工作 :) 10.10,使用 Synaptic pkg 管理器安装了 boost-dev 包 1.40。我是 linux 的新手,这个 boost pkg。我尝试创建一个新项目,然后写:

#include <boost/thread.hpp>
int main(int argc, char* argv[]){
}

I did not include anything or write anything like pthread anywhere. when trying to build, it says:

我没有在任何地方包含任何东西或写任何类似 pthread 的东西。尝试构建时,它说:

/usr/include/boost/config/requires_threads.hpp:47: error: #error "Compiler threading support is not turned on. Please set the correct command line options for threading: -pthread (Linux), -pthreads (Solaris) or -mthreads (Mingw32)"
In file included from /usr/include/boost/thread/thread.hpp:12,
                 from /usr/include/boost/thread.hpp:13,
                 from ../main.cpp:8:
/usr/include/boost/thread/detail/platform.hpp:67: error: #error "Sorry, no boost threads are available for this platform."
In file included from /usr/include/boost/thread.hpp:13,
                 from ../main.cpp:8:
/usr/include/boost/thread/thread.hpp:19: error: #error "Boost threads unavailable on this platform"

and so on, a lot of more errors related to boost. I tried to add -pthread, -pthreads, -lpthreadto where I thought I could, but did not solve the problem. I forgot to mention, that I try to build the project in eclipse, I don't work in the command line, but I also tried g++ -pthread main.cpp and it gives the exact same error. Please give detailed or stepbystep solution, because some things you answer here are simply chinese to me. I just want to get a simple thing running and don't even understand the problem. Don't even understand that error message, what does it want me to do. basically what I did: install eclipse, write the above things in a new project, install libboost 1.4 using sinaptic pkg manager, and restarted everything and tried to compile. I got the error. Don't see what's happening, or what am I missing. (I have libc-dev) Stack is really flowing over now. need some sleep to cool down. thank you guys for help!

等等,还有很多与 boost 相关的错误。我试图添加-pthread, -pthreads,-lpthread到我认为可以的地方,但没有解决问题。我忘了提及,我尝试在 eclipse 中构建项目,我不在命令行中工作,但我也尝试了 g++ -pthread main.cpp,它给出了完全相同的错误。请给出详细的或逐步的解决方案,因为您在这里回答的某些内容对我来说只是中文。我只是想让一个简单的东西运行起来,甚至不明白这个问题。甚至不明白那个错误信息,它要我做什么。基本上我做了什么:安装eclipse,在一个新项目中编写上述内容,使用sinaptic pkg manager安装libboost 1.4,然后重新启动一切并尝试编译。我得到了错误。不要看到发生了什么,或者我错过了什么。(我有 libc-dev)堆栈现在真的流过了。需要睡一会冷静下来。

回答by Erik

As the error message say: pass -pthreadto the compiler:

正如错误消息所说:传递-pthread给编译器:

g++ -pthread yourfile.cpp

Also, for debian, make sure you have libc-devinstalled.

另外,对于 debian,请确保您已libc-dev安装。

回答by orlp

#include <boost/thread.hpp>

int main(int argc, char *argv[]) {
    return 0;
}

Compile with g++ test.cpp -pthread -lboost_thread.

g++ test.cpp -pthread -lboost_thread.

回答by mydaemon

The issue is a well known one from such an old version of boost. You are compiling with gcc/g++ 4.7, where the reference to pthreads have changed to GLIBCXX_HAS_GTHREADS, so boost is unable to find pthreads and disable it.

这个问题是来自如此旧版本的 boost 的一个众所周知的问题。您正在使用 gcc/g++ 4.7 进行编译,其中对 pthreads 的引用已更改为 GLIBCXX_HAS_GTHREADS,因此 boost 无法找到 pthreads 并禁用它。

So you have two options:

所以你有两个选择:

1) Upgrade boost_dev to last release (I think it's fixed in 1.50+).

1) 将 boost_dev 升级到最新版本(我认为它在 1.50+ 中已修复)。

2) Patch your boost include files (I've done this); just edit

2)修补你的boost包含文件(我已经完成了);只是编辑

"your boost folder"/include/config/stdlib/libstdcpp3.hpp

“你的 boost 文件夹”/include/config/stdlib/libstdcpp3.hpp

and change:

并改变:

#ifdef __GLIBCXX__ // gcc 3.4 and greater:
#  if defined(_GLIBCXX_HAVE_GTHR_DEFAULT) \
        || defined(_GLIBCXX__PTHREADS)
      //
      // If the std lib has thread support turned on, then turn it on in Boost
      // as well.  We do this because some gcc-3.4 std lib headers define _REENTANT
      // while others do not... 
      //
#     define BOOST_HAS_THREADS
#  else
#     define BOOST_DISABLE_THREADS
#  endif

to add the new directive in the condition:

在条件中添加新指令:

#ifdef __GLIBCXX__ // gcc 3.4 and greater:
#  if defined(_GLIBCXX_HAVE_GTHR_DEFAULT) \
        || defined(_GLIBCXX__PTHREADS) \
        || defined(_GLIBCXX_HAS_GTHREADS) // gcc 4.7 
      //
      // If the std lib has thread support turned on, then turn it on in Boost
      // as well.  We do this because some gcc-3.4 std lib headers define _REENTANT
      // while others do not...  
      //
#     define BOOST_HAS_THREADS
#  else
#     define BOOST_DISABLE_THREADS
#  endif

Bug description and fixes for linux and windows here:

此处为 linux 和 windows 的错误描述和修复:

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

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

Enjoy.

享受。

回答by nonameentername

You need to install the boost thread libraries:

您需要安装 boost 线程库:

sudo apt-get install libboost-thread-dev