C++ 在 ubuntu 上使用 Boost
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/846566/
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
Using Boost on ubuntu
提问by Kredns
I've heard a lot of good comments about Boost in the past and thought I would give it a try. So I downloaded all the required packages from the package manager in Ubuntu 9.04. Now I'm having trouble finding out how to actually use the darn libraries.
过去我听过很多关于 Boost 的好评,我想我会试一试。所以我从 Ubuntu 9.04 的包管理器下载了所有需要的包。现在我很难找到如何实际使用这些该死的库。
Does anyone know of a good tutorial on Boost that goes all the way from Hello World to Advanced Topics, and also covers how to compile programs using g++ on ubuntu?
有谁知道关于 Boost 的一个很好的教程,从 Hello World 到 Advanced Topics,还介绍了如何在 ubuntu 上使用 g++ 编译程序?
回答by Kyle Simek
Agreed; the boost websitehas good tutorials for the most part, broken down by sub-library.
同意;boost 网站大部分都有很好的教程,按子库细分。
As for compiling, a good 80% of the library implementation is defined in the header files, making compiling trivial. for example, if you wanted to use shared_ptr's, you'd just add
至于编译,80% 的库实现都定义在头文件中,使得编译变得微不足道。例如,如果你想使用 shared_ptr's,你只需添加
#include <boost/shared_ptr.hpp>
and compile as you normally would. No need to add library paths to your g++ command, or specify -llibboost. As long as the boost directory is in your include path, you're all set.
并像往常一样编译。无需向 g++ 命令添加库路径,或指定 -llibboost。只要 boost 目录在你的包含路径中,你就已经准备好了。
From the boost documentation:
从提升文档:
The only libraries that need to be compiled and linked are the following:The only Boost libraries that must be built separately are:
- Boost.Filesystem
- Boost.IOStreams
- Boost.ProgramOptions
- Boost.Python (see the Boost.Python build documentation before building and installing it)
- Boost.Regex
- Boost.Serialization
- Boost.Signals
- Boost.Thread
- Boost.Wave
A few libraries have optional separately-compiled binaries:
- Boost.DateTime has a binary component that is only needed if you're using its to_string/from_string or serialization features, or if you're targeting Visual C++ 6.x or Borland.
- Boost.Graph also has a binary component that is only needed if you intend to parse GraphViz files.
- Boost.Test can be used in “header-only” or “separately compiled” mode, although separate compilation is recommended for serious use.
唯一需要编译和链接的库如下: 唯一必须单独构建的Boost库是:
- Boost.Filesystem
- Boost.IOStreams
- Boost.ProgramOptions
- Boost.Python(在构建和安装之前请参阅 Boost.Python 构建文档)
- Boost.Regex
- Boost.Serialization
- 升压信号
- Boost.Thread
- Boost.Wave
一些库具有可选的单独编译的二进制文件:
- Boost.DateTime 有一个二进制组件,只有在您使用其 to_string/from_string 或序列化功能,或者您的目标是 Visual C++ 6.x 或 Borland 时才需要。
- Boost.Graph 还有一个二进制组件,只有在您打算解析 GraphViz 文件时才需要它。
- Boost.Test 可以在“header-only”或“separatelycompiled”模式下使用,尽管建议在认真使用时单独编译。
So, if you're using one of the listed libraries, use the Getting Started guideto, well, get you started on compiling and linking to Boost.
因此,如果您正在使用列出的库之一,请使用入门指南开始编译和链接到 Boost。
回答by David Cooper
回答by AFoglia
The library documentation is a mixed bag. Some is good, but some is more of a reference than a guide. The best guide to (some of) the Boost libraries is the book Beyond the C++ Standard Library. At the very least, the introduction gives one paragraph descriptions of many of the libraries. From there, you can decide which library is most important for your current needs, and, if it's in the book, read the chapter on it, or read the documentation on the website.
图书馆文档是一个混合包。有些是好的,但有些更像是参考而不是指南。Boost 库(部分)的最佳指南是Beyond the C++ Standard Library一书。至少,介绍部分对许多库进行了一段描述。从那里,您可以决定哪个库对您当前的需求最重要,如果它在书中,请阅读其中的章节,或阅读网站上的文档。
If you read German, there's a good online guide. Google translate does a good enough job that a non-speaker like me can understand it.
如果您阅读德语,这里有一个很好的在线指南。谷歌翻译做得足够好,像我这样不会说话的人也能理解。
Also, unless you have lots of experience with C++, I'd start with the simpler libraries (e.g. smart_ptr, tuple, conversion, tokenizer, regex, date_time, test), before trying the more complicated ones (bind, variant, any), or the really advanced ones (concepts, MPL, Fusion).
此外,除非您对 C++ 有很多经验,否则在尝试更复杂的库(绑定、变体、任何)之前,我会从更简单的库(例如 smart_ptr、tuple、conversion、tokenizer、regex、date_time、test)开始,或者真正先进的(概念、MPL、Fusion)。
回答by CW Holeman II
Using Easypeasy 1.1 (for netbooks) which is based upon Ubuntu I was able to use Synaptic Package Manager to install, I believe, libboost-dev. Then simply by adding:
使用基于 Ubuntu 的 Easypeasy 1.1(用于上网本),我相信我能够使用 Synaptic Package Manager 来安装 libboost-dev。然后只需添加:
#include "boost/foreach.hpp"
I was able to replace the existing lines in an existing application (which has an Ask class which has nothing to do with boost):
我能够替换现有应用程序中的现有行(它有一个与 boost 无关的 Ask 类):
for (std::vector<Ask*>::iterator ii=ui.begin(); ii!=ui.end(); ++ii)
std::cout << (*ii)->prompt() << (*ii)->answer() << std::endl;
with:
和:
BOOST_FOREACH (Ask* ii, ui)
std::cout << ii->prompt() << ii->answer() << std::endl;
As I understand it this is a header only feature. I have not used anything requiring link time changes yet.
据我了解,这是仅标题功能。我还没有使用任何需要更改链接时间的东西。
回答by Balaji
回答by authchir
The best tutorial I've read so far are those two books:
到目前为止,我读过的最好的教程是这两本书:
回答by lothar
The libraries come with documentation and many of them have tutorials as part of the documentation. Just start reading.
这些库随附文档,其中许多库都包含教程作为文档的一部分。刚开始阅读。
回答by coppro
Boost is not a programming language nor an application framework - because it's just a collection of libraries, there is no such thing as a Boost 'Hello World' program. Most libraries in Boost can be used more or less independently, and they vary in size from one function to massive libraries that could stand alone.
Boost 既不是编程语言也不是应用程序框架 - 因为它只是库的集合,所以没有 Boost 'Hello World' 程序这样的东西。Boost 中的大多数库或多或少都可以独立使用,它们的大小从一个函数到可以独立的大型库不等。
The best way to get to know Boost is simply to try and work it in as you write new code. Use smart_ptr
whenever you can; use the MPL next time you want to do compile-time work. There's a lot of variety in Boost, but you should probably start looking at the Utility section; those are the lightest-weight and most commonly-used libraries.
了解 Boost 的最好方法就是在编写新代码时尝试使用它。smart_ptr
尽可能使用;下次要进行编译时工作时,请使用 MPL。Boost 中有很多种类,但您可能应该开始查看实用程序部分;这些是最轻量和最常用的库。