LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Basic usage of Boost, how to compile? (https://www.linuxquestions.org/questions/linux-software-2/basic-usage-of-boost-how-to-compile-4175591787/)

rdx 10-19-2016 08:07 PM

Basic usage of Boost, how to compile?
 
I just installed the latest Boost on my Slackware64 14.1 system. It seemed to go well and as far as I know all got installed properly except the includes and lib were in a weird place. I moved the includes to /usr/local/include and linked the libs to /usr/local/lib to make it generally compatible with my setup.

But when I try to compile one of the example programs I get errors of the type indication a missing lib (i.e., undefined reference to `boost::random::random_device::random_device' and similar in this particular example). From what I read in the manual, mostly libs aren't even needed, the headers supply everything. Basically all I wanted from Boost was elliptic integrals and other special functions but I'm stuck on a simple 'how to compile' problem.

norobro 10-19-2016 10:33 PM

Did you try linking against the library?
Code:

$ g++ main.cpp -lboost_random
The following code from the Boost docs compiles on my 14.2 system:
Code:

#include <iostream>
#include <boost/random/random_device.hpp>
#include <boost/random/uniform_int_distribution.hpp>

int main() {
    std::string chars(
        "abcdefghijklmnopqrstuvwxyz"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "1234567890"
        "!@#$%^&*()"
        "`~-_=+[{]}\\|;:'",<.>/? ");
    boost::random::random_device rng;
    boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
    for(int i = 0; i < 8; ++i) {
        std::cout << chars[index_dist(rng)];
    }
    std::cout << std::endl;
}


rdx 10-22-2016 10:00 PM

Ok, that works, thank you. But I still haven't found in the docs what the libs are and when they are needed. I guess I always have this problem when I'm new to something. It's not that I mind feeling stupid, but I don't have time to spend on things that should be easy, I am busy already, lol.

BTW, the program compiles and runs but does it do anything useful? ^.^

Oh, one more thing: The example programs in the package compile without linking libs. Something magic seems to be going on there but IDK what it is.

genogebot 10-23-2016 02:55 AM

Most of the boost library is just header files, and they don't require building or linking to be used. Just include the appropriate headers and you're good to go.The boost library documentation will tell you which headers you need to include for each library.

norobro 10-23-2016 10:19 AM

You're welcome.

Quote:

Originally Posted by rdx (Post 5621657)
But I still haven't found in the docs what the libs are and when they are needed.

http://www.boost.org/doc/libs/1_62_0...only-libraries


All times are GMT -5. The time now is 07:21 AM.