Friday, July 26, 2013

Boost.Build (Part 3)

This entry is about how to link in an external library using bjam AND a little start into using boost asynchronous IO. I'm not going to cover asio programs because I feel that the blog in the references already covers this extremely well. The entries I'll be making aim to tackle troubleshooting and filling in information where it appears to be missing.

The tutorial in [1] breaks the programs down really well. However there were no instructions on how to build the programs! You won't find a libboost_asio* on your system because that's not what it relies on. The command I used to compile the example using g++ 4.7 was:

g++ main.cpp -L/usr/boost -lboost_system -pthread

That was the absolute minimum to get the test program to build. Please see the developer's blog in [1] for the small example- it's the equivalent of a "Hello World".

Using bjam it's as simple as:

lib boostsystem
    :
    : <file>/usr/lib/libboost_system.so.1.46.1
    ;

exe sampleProgram : main.cpp .//boostsytem :
   <threading>multi ;

I'm still a little confused about the syntax of a Jamroot, so I looked it up [2]. To break things down line by line:

lib boostsystem

This line specifies that we have a library that we are describing "lib" (this is known as a rule-name) and its identifier is "boostsystem". The identifier could have been anything but since I'm linking together an ASIO program boostsystem is very descriptive.

:

The items after the first colon in a specification are the "sources" or input files. Since an existing library doesn't really have source inputs its contents will be empty.

: <file>/usr/lib/libboost_system.so.1.46.1

The contents after the second colon are the requirements. I'm not sure what the file tag does in this case- but it probably reads that the file must exist in order for the specification to be met.

;

Simply the termination of the specification.

exe sampleProgram : main.cpp .//boostsystem :

This line specifies that we are describing an executable program "exe" and that it is called "sampleProgram". Notice that the first colon is on the same line and that the first area specifies inputs as main.cpp (the source file) and .//boostsystem (which is the library we specified first!). The sources are terminated with a colon.

<threading>multi ;

Remember how in the manual command line execution I specified -pthread? Adding this argument to the build does that for you. This is a cool way to do it especially if you are working across different platforms where the threading platform differs (Windows vs. Linux vs. BSD's). Simply invoke bjam in the same directory and you're all done! Your first ASIO program is now built.


REFERENCES

[1] - http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio/

[2] - http://docs.huihoo.com/boost/1-33-1/doc/html/bbv2/advanced/jamfiles.html

No comments:

Post a Comment