Using Boost on ubuntu

2019-02-03 10:31发布

问题:

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.

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?

回答1:

Agreed; the boost website has good tutorials for the most part, broken down by sub-library.

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

#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.

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.

So, if you're using one of the listed libraries, use the Getting Started guide to, well, get you started on compiling and linking to Boost.



回答2:

The Boost website has some good tutorials, they are just kind of hidden.



回答3:

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.

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).



回答4:

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:

#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):

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.



回答5:

I was just looking at that german boost guide, and found there was an english one as well (same book). It looks pretty good, have just read the introductory pages which are quite useful



回答6:

The best tutorial I've read so far are those two books:

  • Introduction to the Boost C++ Libraries; Volume I - Foundations
  • Introduction to the Boost C++ Libraries; Volume II - Advanced Libraries


回答7:

The libraries come with documentation and many of them have tutorials as part of the documentation. Just start reading.



回答8:

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.

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.