Labels

Android (1) bash (2) boost (2) C (34) C++ (2) cheatsheet (2) CLion (6) css (3) Debian (33) DL (17) Docker (2) Dreamweaver (2) Eclipse (3) fail2ban (4) git (5) GitHub (4) Hacking (3) html (8) http (1) iOS (1) iPad (1) IRC (1) Java (31) javascript (3) Linux (167) Mac (19) Machine Learning (1) mySQL (48) Netbeans (4) Networking (1) Nexus (1) OpenVMS (6) Oracle (1) Pandas (3) php (16) Postgresql (8) Python (9) raid (1) RedHat (14) Samba (2) Slackware (47) SQL (14) svn (1) tar (1) ThinkPad (1) Virtualbox (3) Visual Basic (1) Visual Studio (1) Windows (2)

Friday, 26 June 2020

C++ Boost Libraries

Usage examples of the c++ boost libraries


#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " " );
}

The above can be compiled as follows:

g++ -I /usr/local/Cellar/boost/1.72.0_3 main.cpp -o main.o

Where;
the -I option is used to pass the path to the boost installation.
main.cpp is the name of the source file to be compiled.

To use the above example do;

echo 1 2 3 | ./main.o (where main.o is the binary)


Link Your Program to a Boost Library 

(we need to do this if the boost library has a separately-compiled binary component)


Pay attention to some detail here:
https://www.boost.org/doc/libs/1_73_0/more/getting_started/unix-variants.html#id27


#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

    while (std::cin)
    {
        std::getline(std::cin, line);
        boost::smatch matches;
        if (boost::regex_match(line, matches, pat))
            std::cout << matches[2] << std::endl;
    }
}

The above can be compiled in one of two ways;

1) #########################################

This method is bet suited if there are more than one library used, but by default it may choose the dynamic *.so or *.dylib unless we add the -static option to the commandline line.

g++ -I /usr/local/Cellar/boost/1.72.0_3 main2.cpp -o main2.o -L /usr/local/Cellar/boost/1.72_03/lib/ -l boost_regex

Where;
the -I option is used to pass the path to the boost installation.
main2.cpp is the name of the source file to be compiled.
main2.o is the output binary
-L is the directory to search for the binaries, such as *.a or *.dylib
-l is the name of the library without the leading lib and without the trailing suffix (.a or .dylib etc)

Note: *.a is a static library while *.so is a dynamic library (*.dylib on Mac)

Note: adding the -static option to the compiler (this didn't work with the above example on Mac)

2) #########################################

This method is bet suited if there is only one library used.

g++ -I /usr/local/Cellar/boost/1.72.0_3 main2.cpp -o main3.o /usr/local/Cellar/boost/1.72.0_3/lib/libboost_regex.a

Where;
the -I option is used to pass the path to the boost installation.
main2.cpp is the name of the source file to be compiled.
main3.o is the output binary
followed buy the full  path to the library itself (*.or *.dylib etc)

In the above instance, both *.a and *.dylib worked on Mac


No comments:

Post a Comment

Note: only a member of this blog may post a comment.