How can I use just one boost library/file? [duplic

2020-05-27 04:21发布

问题:

This question already has answers here:
Closed 10 years ago.

Possible Duplicate:
Building a subset of boost in windows

I'm using Visual c++ 6.0, and I'd like to use boost::random. I can't find any examples showing how I would go about just using this and only this library. Can I just include it in my source?

回答1:

Boost got a very nice tool called bcp which does what you want. check it out.

from the docs:

bcp boost/regex.hpp /foo

Copies boost/regex.hpp and all dependencies including the regex source code 
(in libs/regex/src) and build files (in libs/regex/build) to /foo.  Does not
copy the regex documentation,  test, or example code.


回答2:

The only thing to be careful about is to add the path to boost in your include path. Even though the libraries are headers only, many boost headers include other headers in the boost library and won't be able to find them if you don't have the include path set up correctly.

My current project has this include path:

/I "C:\Program Files\boost\boost_1_39"

My code then includes boost headers like this:

#include <boost/random.hpp>

boost/random.hpp has a bunch of lines like #include "boost/random/linear_congruential.hpp", which is why you need the include path.



回答3:

Most of boost is distributed as "header only" libraries - meaning you do not need to "build" any kind thing to use that library. Random is one of those, so it is possible to just include the correct header files and you will be away laughing. I would say it is best to get the whole boost source somewhere, and reference that since the different parts tend to have some dependencies on each other.



回答4:

Yes, most boost libraries are header only so you do not need to compile any source files.



回答5:

Yes, to use boost::random, you can simply include the header in your source file.

Since Boost uses templates extensively, many of the libraries are implemented purely in headers (and template implementation files, a la txx).

Most of the libraries are also independent, so you can use just one without depending on another. (Exceptions are documented.)

If you look at the list of Boost libraries you will see many of them labelled "Header only", so there is no additional library to link against.



回答6:

how I would go about just using this and only this library

I am not sure what your concern is here. Even if you included other headers, and/or linked other libraries, your final executable will include only those parts of the library needed to resolve symbols in your code. It will not throw in unused object modules. Inlined code and templates in headers are not instantiated unless they are referenced.

Since Boost is primarily a template library and to work around lack of template awareness in most linkers, the code is inlined in the header file, the instantiated templates are compiled into the each object module in which it is instantiated. So you may end up with duplicate code in multiple modules, which if executable size if your concern, you should perhaps be aware of.