Reputation: 37058
I'm very inexperienced with Linux and the terminal, but I'm trying to learn. I've also never included an external library before. Now I need to include the Boost.Asio library in a program being developed in Ubuntu with G++.
Could someone very kindly and very carefully explain how to go about this, from the beginning?
EDIT:
Expanding on the original question: if I need to send this code to someone else for them to run it on a completely separate machine but in the same environment, how do I take that into account? If this whole process involves literally placing library files into the same folder as the code, do I just send those library files along with the .cpp to this other person?
Upvotes: 2
Views: 3032
Reputation: 294267
You don't include the library, but instead you declare a dependency on it. Eg. consider you use autoconf and automake then you would add AX_BOOST_BASE
1 to require boost and AX_BOOST_ASIO
to require the ASIO libraries. In your Makefile.am
file(s) you use BOOST_CPPFLAGS
and BOOST_LDFLAGS
macros and rely on the ./configure to set them properly. Then whoever consumes your code will have to run the well know ./configure
script which will analyze the environment for the location of boost and setup appropriate values in the build environment so that the make
succeeds.
Well at least this is the theory. In practice there is a reason the whole thing is better known as autohell. Alternatives exists, like CMake or boost's own bjam. But the synopsis is always the same: you declare the dependency in your build configuration and the destination location that consumes you product has to satisfy the requirement (meaning it has to download/install the required version of boost, in your case). Otherwise you enter into the business of distributing binaries and this is frowned with problems due to richness of platforms/architectures/distributions your application is expected to be deployed in.
Obviously if you use a different build system, like ANT, then you should refer to that build system documentation on how to declare the requirement for boost.
1: ax_boost.m4
is not the only boost detecting m4 library, there are other out there, but is the one documented on the GNU autoconf list of macros
Upvotes: 0
Reputation: 2200
You have mentioned you are using Ubuntu, so the simplest way to use boost is to first install libboost-all-dev
package (from synaptic), which will install everything for you including those that needed to be compiled. Then you just need to use g++
in the usual way.
Please note that whether the version is what you want, if not, you may want to install it yourself. On the other hand, boost is mostly header only library, so you only need to extract the files (right click in Ubuntu...) to a folder and link to it while compiling:
g++ hello_world.cpp -I boost_1_49_0/boost
where the last one specify the path for compiler to find the boost headers (please use absolute path).
If you want to send your program to others, dont copy only some boost files, it does not work because of the dependence. Ask them to install the same environment as you while is easy (just unzip a file...).
Upvotes: 1
Reputation: 210445
I don't know about your specific IDE, or about Boost.Asio specifically, but in general:
Whenever you need to link to a library, there is a file named similar to lib???.a
, which you need. You need to pass the -l???
flag to g++
to link to the file.
(I'm not too familiar with the details myself, so there might be other file formats and whatnot too...)
Regarding the edit:
The "right" way would be to just have them download the library themselves, and just pass -l???
to their linker. Including Boost in your source code will make it huge, and for no good reason... it's not like you include the STL in your code, after all.
Upvotes: 0