Compiling with individual Boost libraries, Without

2019-04-08 14:43发布

I'm implementing on some C++ code that I would like to make as portable as possible. I would like to avoid dependencies on libraries that require root access to install. Further, I'd prefer to avoid keeping copies of large libraries in my repository, and I would also prefer not to do user-level installations of libraries (simply because I'd have to manually install them on multiple computers.)

I would like to use the normal_distribution functionality from Boost in my project. I understand that the typical way to install Boost requires a sudo apt-get or sudo yum type of command, but I don't have root access on the systems where this code will run. To get around this, I'm wondering if I could simply place a copy of Boost's normal_distribution.cpp and normal_distribution.hpp in my code directory and compile/link my code with these files. Would this work?

Readers may wonder why I'm not just using the normal_distribution implementation in TR1 or C++11. The answer is that I need to maintain compatibility with some university-managed clusters that still run g++ 4.1.x, which (at least in my experience) doesn't support <TR1/random>.

3条回答
放我归山
2楼-- · 2019-04-08 14:48

I imagine that BCP (Boost Copy) was written precisely with your situation in mind.

查看更多
Viruses.
3楼-- · 2019-04-08 14:50

I had some trouble getting BCP to work properly. I documented the BCP-related question in this StackOverflow post.

For the short term, I just added a normal_distribution function to my codebase. I put it together from a couple of past StackOverflow posts. This implementation doesn't do any fancy template stuff, but it otherwise looks pretty similar to the Boost, TR1, and C++11 APIs of normal_distribution.

#include "math.h"
double normal_distribution(double mean, double stdDev)
{
    //scale the number to appropriate distribution 
    return mean + (sampleNormal()*stdDev);
}

//get a number from normal distribution (mean=0, stdDev=1). 
double sampleNormal() {
    double u = ((double) rand() / (RAND_MAX)) * 2 - 1;
    double v = ((double) rand() / (RAND_MAX)) * 2 - 1;
    double r = u * u + v * v;
    if (r == 0 || r > 1) return sampleNormal(); //recursively re-generate number if doesn't meet criteria
    double c = sqrt(-2 * log(r) / r);
    return u * c;
}

Thanks to this StackOverflow post (user Pete855217) for the sampleNormal() function, and thanks to this StackOverflow post (user5084) for the function that I've named normal_distribution().

查看更多
冷血范
4楼-- · 2019-04-08 15:05

You should check out the ryppl project as this is exactly what it is hoping to achieve. If you follow the links to github you will find modularised boost and from there you may go on and try a full ryppl install. In any case there is a half way house and that is modularised boost. There is also a modularised boost/cmake to make it simpler. This is the direction ryppl is looking to take boost and it should be very helpful. The only downside I can see is the propensity to use python scripts for the zero install installer.

As stated BCP was developed for this purpose as well so there is a choice. Be aware though boost is going through a svn->git change and this seems to be affecting some structure which is reflected in some inconsistencies with the current modularised boost, I am not sure how/if that affects BCP as I have no knowledge of that system.

查看更多
登录 后发表回答