Friday, August 7, 2009

Configuring Boost C++ Library with Visual Studio 2008

I) Download Boost C++ Library

The latest release of Boost library is Version 1.39.0, released in may 2009. This release has the following new libraries and updates over its previous version, Version 1.38.0.

New Libraries : Signals2.

Updated Libraries : Asio, Flyweight, Foreach, Hash, Interprocess, Intrusive, Program.Options, Proto, PtrContainer, Range, Unordered, Xpressive. Updated Tools: Boostbook, Quickbook.

Download path: http://www.boost.org/

II) Integrating Boost C++ libraries with Visual studio 2008

Integration involves two steps. This makes the application utilize the facilities of Boost C++ libraries, and be fully functional.

1) ‘Including’ Boost directory path:-

Steps:-

a) Launch Visual studio 2008.

b) Click on ‘Tools’ menu & select ‘options…’

c) On the popped-up ‘options’ window, towards left, select and expand the directory ‘Projects and Solutions’. In the expanded list select ‘VC++ Directories’.

d) Now towards the right of ‘Options’ window, click on ‘Show directories for:’ dropdown list and select ‘Include files’.

e) Now in the window below, include the path of the Boost C++ ‘root’ directory. To do this, click just below the last included path. With this a cursor appears, where we can type the path, or we can even include the path by clicking on the ellipsis button at the end of the line and browse to the root directory.

f) Finally click on ‘Ok’.

2) ‘Linking’ the Boost libraries with the project.

Steps:-

a) In ‘Solution Explorer’, right click on the project folder and select properties.

b) Now the project properties window pops-up.

c) Towards the left of the properties window, select and expand ‘Configuration properties’ directory.

d) Now select and expand ‘Linker’ directory and click on ‘General’.

e) Now to the right of the ‘Project property’ window, select the property ‘Additional Library directories’ and add the path of Boost C++ Library directory.

(Example: ‘C:\Program Files\boost\boost_1_38\lib’).

f) Finally click ‘Apply’ and ‘Ok’.

III) ‘Build’ and ‘Run’ your application.

Microsoft Pre-defined macros

_MSC_VER

This is the predefined macro, which identifies the Micro

Soft Compiler VERsion. It comprises of the major and minor number components of the compiler’s version number.

Open your Visual studio Command Prompt and type in “cl/” and hit enter. The output in my system is shown below

.

So, the VC++ compiler version of my system is 15.00.21022.08. In this period delimited version number, the major number corresponds to the first component , 15 and the minor number corresponds to the second component, 00. So for Visual studio 2008, the _MSC_VER macro evaluates to 1500.

Below are a couple of examples of the usage of _MSC_VER macro in programming.

Example1:

#ifdef _MSC_VER

//if it is any version of MicroSoft Compiler, the below main() function

// is executed. Else this portion of code will be inactive.

int main(int argc, char* argv[])

{

//some come

return 0;

}

#else

//if it is any other compiler other than MicroSoft, the below main()

//function is executed. Else this portion of code will be inactive.

int main(int argc, char* argv[])

{

//some come

return 0;

}

#endif

In the above code snippet, it is shown that it has two ‘main()’ functions. But out of these two, at any point, only one will be active and the other is totally inactive. The compiler only identifies the main() function relevant to it. There by this does not result in any compiler error.

Example2:

#if (_MSC_VER >= 1400)

//VC 8.0: Executes this part of the code if the Compiler is VC 8.0

// and above.

#elseif (_MSC_VER >= 1310)

// VC 7.1: Executes this part of the code if the Compiler is VC 7.1.

#elseif (_MSC_VER >= 1300)

// VC 7.0: Executes this part of the code if the Compiler is VC 7.0.

#else

// VC 6.0: Executes this part of the code if the Compiler is VC 6.0.

#endif

In the above example a distinction is made between the various versions of Microsoft compiler. So based on the compiler version that part of the code executes and not all.