c++boostinclude

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


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.


Solution

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