Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 552
Roman Avatar asked Jun 19 '13 07:06

Roman


People also ask

Does Boost work with C?

Boost can't be used with C as it uses OOP features from C++. It may be technically possible to develop a wrapper for it.

How do I add a Boost library?

In the properties dialog, select "Configuration Properties" and then "VC++ Directories". You will need to add the Boost include path to the "Include Directories" list. If you're using all header-only libraries then you're done. Otherwise, you will need to add the Boost library path to "Library Directories".

How do I use Boost library in code blocks?

Download either the zip or the 7zip package of boost. Extract the contents to extract_dir. If the minGW\bin folder (can be found in CodeBlocks installatoin folder) is not in the path variable add it. Open the file extract_dir\project-config.


2 Answers

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.

like image 184
Rody Oldenhuis Avatar answered Oct 02 '22 07:10

Rody Oldenhuis


(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.)?
like image 31
Yehezkel B. Avatar answered Oct 02 '22 09:10

Yehezkel B.