I recently started using boost libraries in a C++ project. Wanting to reduce overheads when compiling projects, I decided to build and link the libraries in Visual Studio. There were a few problems when following the instructions on boost (for starters, we are building 1.68, not 1.67). These problems and my solutions are detailed here.
Getting and building boost
- Download 1.68.0 (download link). The rest of these steps assume it is downloaded to the default directory
- Unzip it (if you don’t have it, get 7zip here)
- Launch cmd prompt in the unzipped directory (you won’t need Administrator privileges)
This will create the build libraries in
The last lines of output from Step 5 should say:
The Boost C++ Libraries were successfully built!
The following directory should be added to compiler include paths:
The following directory should be added to linker library paths:
And this is far more help than you get in the documentation!
Linking to boost in Visual Studio
I copied and pasted the
C:\Users\username\Downloads\boost_1_68_0\stage directories into
C:\Program Files\boost\boost_1_68_0. This is the path suggested in the boost getting started guide. It does not appear that you need any other of the files from the boost directory that was downloaded and built.
These need to be linked in the following places in the properties of a project in Visual Studio:
This should then enable you to start using boost! I’ve created a starter project on Github so that the above steps don’t need to be repeated again. It uses the simple regex example from the starter guide to show that the libraries have been correctly linked.
One issue, which I will write a separate article about, is that Visual Studio creates its initial source code files in any project in UCS-2 LE BOM encoding. This means that Git does not pick it up as a text file and assumes it is binary, making code changes unmanageable. I still haven’t gotten to the bottom of why this occurs, but it can be fixed by opening the file in Notepad++ and changing the encoding to UTF-8 from the Encoding menu.
Other than this, for the example regex program, the steps outlined here are enough. However, I started a project using GIL and immediately ran into more problems.
There are two errors that need to be overridden with preprocessor definitions. “wcstombs may be unsafe”, and problems with std::uninitialized_copy and std::copy, somewhere in the boost libraries. These are solved by adding _CRT_SECURE_NO_WARNINGS and _SCL_SECURE_NO_WARNINGS to preprocessor definitions as the error output suggests.
The last problem is an unexpected end of file error. This is solved by turning off using precompiled headers in the project properties.