Linking against Boost libraries on Linux
If you have installed Boost in a nonstandard location (which is typically the case if you have not installed it from a native package), then you will need to make sure that your preprocessor can find the Boost header files you have included using the –I
option in the compiler:
This step will create an object file called chkfile.o
, which we will link to the binary. You can specify which library to link to using the -l
option. In case of a nonstandard installation, you will need to ensure that the linker can find the path to the library you want to link against using the -L
option:
Note
Use the -std=c++11
option only if you built your Boost libraries using C++11.
The preceding command line will work for either a static or a shared library. However, if both types of library are found, it will use the shared version. You can override this with appropriate linker options:
In the preceding case, the filesystem
library is linked statically while others are linked dynamically. The -Wl
switch is used to pass its arguments to the linker. In this case, it passes the -Bstatic
and -Bdynamic
switches.
If it is a shared library that you link against, then at runtime the dynamic linker needs to locate the shared library and load it too. The way to ensure this varies from one version of Unix to the other. One way to ensure this is to embed a search path in your executable using the rpath
linker directive:
On the target system, where the binary mytest
is run, the dynamic linker would look for the filesystem
and system
shared libraries under /opt/boost/lib
and /usr/lib/boost
.
Other ways besides using the rpath
mechanism also exist. Linux uses a utility called ldconfig
to locate shared libraries and update search paths. For more details, look at the man pages for ldconfig (8)
. On Solaris, the crle
utility performs a similar action.
Linking against Boost libraries on Windows
Using the Visual Studio IDE, we will have to tweak certain project settings in order to link against the Boost libraries.
First, ensure that your compiler is able to find the necessary header files:
- Open your C++ project in Visual Studio. From the menu, select Project | Project Properties.
- In the Property Pages dialog that comes up, expand Configuration Properties and select C/C++.
- Edit the value of Additional Include Directories by adding the path to your Boost, include directories. Separate it from other entries in the field using a semicolon:
- Next, ensure that your linker is able to find the shared or static libraries. In the Project Properties dialog, under Configuration Properties, choose Linker.
- Edit the Additional Library Directories field to add the path to the Boost libraries, separated by a semicolon from any other entries:
- Now you can leverage Boost's auto-linking feature on Windows to automatically link to the correct libraries. To enable this, you have to define the
BOOST_ALL_DYN_LINK
preprocessor symbol. To do this, in the Project Properties dialog, navigate to Configuration Properties | C/C++ | Preprocessor, and add BOOST_ALL_DYN_LINK
to the Preprocessor Definitions field, separating it from other entries with a semicolon.
If you built your Boost libraries on Windows with the default layout (versioned), this is all you will need to do for linking correctly. If we use the tagged layout, we must also define a second preprocessor symbol BOOST_AUTO_LINK_TAGGED
. If we use system layout for naming, we will need to define BOOST_AUTO_LINK_NOMANGLE
instead. You will get a linker error without these definitions:
You should now be able to build your project from your IDE without any problems. In order to run your program, the dynamic linker must be able to locate the dynamic library. To take care of this, on Windows, you can add the path of your Boost libraries to the PATH environment variable. For running your programs from within the IDE, you can add the path of your Boost libraries to the PATH variable by navigating to Debugging | Environment, as shown in the following screenshot: