How to include the boost library into a C++ program?

40,366

Solution 1

You need an include path in your g++ command:

g++ -I/opt/software/boost/1.45_ubuntu12.4lts_gcc4.5.3/include/  [rest of command here]

(and possibly a link to a library path as well).

In general, it's not a good idea to put full paths in your source code; that kind of completely destroys the idea of portability :) (meaning, that code can no longer be compiled on any other PC in the world than your own, and even that is going to be dubious half a year from now).

Anyway, if you find yourself typing long compiler lines like the one above, it's really time to start using a makefile.

You'll probably find this question interesting as well.

Solution 2

(This isn't a direct answer to the question, but a list of considerations that I think should be addressed with the final and complete answer that @uoɥʇʎPʎzɐɹC wants to see here.)

The question of handling 3rd party dependencies with C++ isn't a simple one. There are many approaches for this and choosing the approach that is right for you depends on your toolset and environment, on your project management and on the trade-offs you want to take.

For Boost, we have to remember that it's mostly header-only library, but some components include a separately-compiled part too (can be static or dynamic lib, can be mandatory for the component or just for a specific use-case of it). E.g. Boost.Filesystem requires compilation, Boost.Graph requires it only if you want to parse GraphViz files and Boost.Variant doesn't need it at all (is "header-only" library). For details, see http://www.boost.org/doc/libs/release/more/getting_started/unix-variants.html#header-only-libraries (this redirects to the latest version, which is currently 1.61).

Using only header-only parts of Boost simplifies many of the considerations, but, of course, sometimes you need the other parts too.

Things to consider:

  1. Are you using only header-only parts of Boost or you need a separately-compiled part too? If you need a separately-compiled part, do you use static lib or dynamic lib? If you use separately-compiled part and want to use dynamic lib, you have to decide how to find the dynamic lib when running the application (especially if you distribute your project as binary).
  2. Is your project distributed as source or as binary? In the binary case, you worry mainly about the developer workflow (but see the point above about using dynamic lib). In the source case, you want it to be easy to compile on every other computer.
  3. Do you want your project to stick with the same version of Boost (at least until you explicitly decide to change version) or you want it to use whatever is installed on the specific machine (assuming there is no API changes)?
  4. Are you OK with having a copy of Boost (or part of it) with the project or you want a central location for all your projects to use?
  5. How much manual config steps you want to force on the users (end users or developers, depends on the other questions above)? (0 is probably preferred, but there is always a trade-off.)
  6. Is your project Windows-only, Linux-only, etc.? Each platform has its own ways and depending on your answer to other questions, the methods you should use can vary between OSes. Cross-platform, for our topic, usually means that you have to implement the relevant approaches for each of the platforms you want to support.
  7. What is your toolset and build environment (e.g. Visual Studio, Qt, make, simple scripts, etc.)?

Solution 3

To specify a directory to search for include files:

-I /opt/software/boost/1.45_ubuntu12.4lts_gcc4.5.3/include

To specify a directory to search for libraries:

-L /opt/software/boost/1.45_ubuntu12.4lts_gcc4.5.3/lib

To specify the actual library name:

-l foo

when your library is called libfoo.a

you don't have to write space after -I, -L or -l for now it is more readable.

HINT:

Use Makefile. Maybe you have the boost include patch already exported to some environment variable.

Share:
40,366
Roman
Author by

Roman

Updated on August 11, 2020

Comments

  • Roman
    Roman over 3 years

    I am trying to compile this small program:

    #include <boost/math/distributions/poisson.hpp>
    
    namespace boost { namespace math {
    
    template <class RealType = double, 
              class Policy   = policies::policy<> >
    class poisson_distribution;
    
    typedef poisson_distribution<> poisson;
    
    template <class RealType, class Policy>
    class poisson_distribution
    { 
    public:
      typedef RealType value_type;
      typedef Policy   policy_type;
    
      poisson_distribution(RealType mean = 1); // Constructor.
      RealType mean()const; // Accessor.
    }
    
    }} // namespaces boost::math
    

    This code is taken from here.

    The compiler tells me that boost/math/distributions/poisson.hpp is not found. So, I try to find this file by myself (using locate poisson.hpp command). I find the following file: /opt/software/boost/1.45_ubuntu12.4lts_gcc4.5.3/include/boost/math/distributions/poisson.hpp. So, in my code I put the full name of the file to make sure that compiler finds it:

    #include </opt/software/boost/1.45_ubuntu12.4lts_gcc4.5.3/include/boost/math/distributions/poisson.hpp>
    

    But now I get another error message: boost/math/distributions/fwd.hpp is not found.

    Is there a way to force the compiler to search the files in the correct directory?

    I use g++ compiler.