VisualStudio C++, Boost C++ and premake

Created
Updated
Length
1041 words · 5 min read
  • programming
  • premake
  • boost-c++

Whenever, we Progrmmers have to deploy a larger project 2 things immediately come to mind (they should come to mind, especially if you’re going work with others too),

  • Source Control
  • Build System

There are several source control are there with git being very popular these days. As for build system there are also too many, coming in large variety flavours CMake, scons, build2 to just name a few.

My favourite is Premake, its super easy to use, and very easily works with Visual Studio. It is well documented, even though its somewhat limited in campatibility campared to other tools in the market, being targetted to fewer common IDEs its more reliable.

Lets discuss how to intigrate Boost C++ libraries and MASM(Microsoft Assembler that comes with Visual studio) to your projects with Premake build system, assuming you have windows computer and visual studio as your IDE.

Note: For Mac users steps be same normally, though try MSDN your IDEs configration, i’m not a Mac user.

So whats the deal, what could go wrong.

SCROLL FURTHER DOWN FOR LINKING YOUR PROJECT TO BOOSTS-GITHUB-REPO

Intigrating Boost (via direct-download)

The best/easiest way to use boost is to, go over [https://www.boost.org/] then download the latest version, extract the zip file to desired location. then follow the steps:

  • Go to main dir of boost dir ex. for me c:/Dev/Cpp_Repos/boost/
  • Open/execute “bootstrap.[bat or sh]” whichever fits your bill, this will deploy b2 build system (boost’s own build system).
  • Open cmd and execute “b2 headers”, this will create directory named “boost” (inside boost root dir). It will cantain shortcuts to all header folders. Many(most) of the boost’s individual libraries are header only so well are technically ready to go but say you boost’s thread lib, then you need to build the corresponding libraries
  • The boost’s document says, use/execute “b2” cammand for building every type of library (ex.: dll dependent, static, thread safe etc.) however that’s not true (it is but technically but lets explore) By default b2 only builds debug binaries(of most types). Note: on some computers(like mine) you have to build library according to your needs.

Its pretty common to have multiple compilers inside your computer. So execute “b2 –help” for details regarding what you can do with this tool, it may seem overwhelming if you’re a novice so don’t worry let’s get relevant ones that are “Properties” and “Options” (or “Important Options”). I’ll skip the details as what each cammands does is already clarified here. what we are interested is SRC(Boost documentation, section4-Invocation/Configration): toolset, address-model(optional), runtime-link, threading, variant (assuming you have MSVC as your compiler, otherwise requirements may vary).

Note: if you are not sure about which MSVC toolset you have then, open any VS-Project -> [Right-click on any of the project in solution explorer, click properties OR click Project in main menu bar on top, click proprties] .. -> Inside “Configration Properties/General” see platform-toolset, ex. v142 means msvc-14.2

in Visual Studio 2019 toolset=msvc-14.2

It’s very important to understand what type of library you require for the project, otherwise you may get linker error saying library not found

Whenever you build boost library, if go to the build/stage/lib, you’ll see a somwhat daunting naming system [Documentation: https://www.boost.org/doc/libs/1_77_0/more/getting_started/windows.html#library-naming]

If you go to VS’ Runtime Library configration, there are 4 Options

\MT  - multi-thread static library linking, release build >> -mt & -s
\MTd - multi-thread static library linking, debug   build >> -mt & -sgd
\MD  - multi-thread shared library linking, release build >> -mt
\MDd - multi-thread shared library linking, debug   build >> -mt & gd

for -mt use threding=multi, for -s use runtime-link=static, for -*gd use variant=debug else variant=release, if you want to be architecture specific use address-model=32 (or 64, arm etc.) else it will build both(32,64)

Ex. b2 toolset=msvc-14.2 threding=multi runtime-link=static variant=release (for /MT)

it will take some time to build… (so chill and drink coffee) Tip: somtimes shell stops working so you may need to press enter(just to wake it up a bit).

now all it needs to do is connect it to your VSProject, for that

  • Go to Project properties -> Configration properties - C/C++ -> General -> Additional Include directories -> ADD Boost’s root “./boost/” dir not “./boost/boost” (remember “b2 headers” which creates shortcuts to all headers inside ./boost/boost/*).
  • Due to Auto-linking feature of IDEs, we don’t need to specify eack and every library individually, all it needs is directory to all the libraries so inside Configration properties - Linker -> General -> Additional Library Directories -> ADD “./boost/stage/lib/” (where your binaries are built/stored)

That’s it !

//---------------------
// main.cpp for test purposes
//---------------------
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>

void wait (int seconds)
{
	boost::this_thread::sleep_for (boost::chrono::seconds{ seconds });
}

void thread ()
{
	for (int i = 0; i < 5; ++i) {
		wait (1);
		std::cout << i << '\n';
	}
}

int main ()
{
	boost::thread t{ thread };
	t.join ();
}

FOR premake:

----------------------------
-- scripts/boostDir.lua to store location of boost
----------------------------
local boost = {} -- module
function boost.Dir() -- absolute path to boost
    return "C:/Dev/Cpp_Repos/boost/"
end
function boost.RelBin() -- relative path to binaries from root of boost
    return "stage/lib/"
end
return boost
----------------------------
-- [Project COPpremake5.lua
----------------------------
boost = require("scripts.boostDir") -- loading module

project "sample"
    includedirs
	{
        "%{boost.Dir()}"
    }
    libdirs "%{boost.Dir()}%{boost.RelBin()}"

=============X============

Intigrating Boost (via github repo clone)

  • Refer to wiki on cloning [https://github.com/boostorg/wiki/wiki/Getting-Started%3A-Overview]
  • It will take quite a bit of time to clone.
  • after cloning go to step-2 of Intigrating Boost (via direct-download) Common problem of mismatch library version: since the github repo is ahead of the releases, so it is common for the built-binaries and required libraries to be of different version and then get the error like Error LNK1104 cannot open file ’libboost_thread-vc142-mt-sgd-x32-1_77.lib' so go to the lib folder “./boost/stage/lib/”, hunt down “libboost_thread-vc142-mt-sgd-x32-X_XX.lib”, if its not there then you gotta build for that purticular configration, the see which version the binary is built for “X_XX” (in my case “1_78”), got to your project, and wherever you are including boost libs add on top:
#include <boost/version.hpp> // go inside version.hpp for more information
#define BOOST_VERSION 107800
#define BOOST_LIB_VERSION "1_78"
    ^ to fix the linker error.

Example:

#include <boost/version.hpp> // go inside version.hpp for more information
#define BOOST_VERSION 107800
#define BOOST_LIB_VERSION "1_78"

#include <boost/thread.hpp>
#include <boost/chrono.hpp>

int main (){
    boost::this_thread::sleep_for(boost::chrono::seconds(60));
}