Posts

Using Win32 static libraries in Visual Studio projects

Image
A static library is simply a file that contains functionality that can be made available to other programs. Static libraries end with the .lib extension and are made available to other Visual Studio projects (console applications, MFC applications etc) by creating links to them. Please follow the steps described below in order to create and use your own static libraries.

How to recursively print STL-based trees

Image
There are plenty of resources on how we may recursively search and print the contents of binary trees. This example shows how to (recursively) make use of the Boost serialization libraries and streams in order to print the contents of a tree-like data structure. For more help on getting set up with the Boost libraries in Visual Studio environments, see these following links: https://www.technical-recipes.com/2012/using-boostpro-to-install-boost-library-packages/ https://www.technical-recipes.com/2011/how-to-install-the-boost-libraries-in-visual-studio/ This following 'Node' class stores a std::string text value. In place of things like linked lists or binary trees, the Node class uses a std::vector of Node objects: [code language="cpp"] #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/vector.hpp> #include <fstream> #include <vector> #include <iomanip...

Using BoostPro to install Boost library packages

Image
UPDATE 15 February 2014: BoostPro is no more. You may find this alternative post useful in setting up the Boost libraries that require separate compilation. A number of Windows-based Boost libraries are not "header-only" and require that you must get them compiled. One way is to compile them yourself . A possibly easier way is to do this via the prebuilt installer packages from BoostPro . Say for example you wish to use the Boost serialize facilities in your program:

How to link DLL files to C++ Projects

Image
These instructions, also available on the Microsoft site , show how to create from scratch a Visual Studio 2010 project that can utilize dll routines created elsewhere, by way of referencing. It basically describes the same thing, as in how to link DLL files but with additional screenshots to make it more intuitive.

Using stateful functors

For another example posts on functors (function objects) see here . A functor is an instance of a C++ class that has the operator() defined. One big advantage of functors is that when you define the operator() in C++ classes you not only get objects that can act like functions, but can also store state as well.

Using boost::bind to assign functions

Some code samples I have collated in the sample below, that demonstrate how boost::function can be assigned with functors, ordinary functions, class member functions and overloaded class member functions respectively. [code language="cpp"] #include <iostream> #include <boost/function.hpp> #include <boost/bind.hpp> using namespace std; // Class for example 1: functors class int_div { public: float operator()(int x, int y) const { return ((float)x)/y; }; }; // Class for example 2: : accessing functions float average( int values[], int n ) { int sum = 0; for (int i = 0; i < n; i++) sum += values[ i ]; return (float) sum / n; } // Class for example 3: accessing class members class DoStuff { public: void DoThis() { std::cout << "Do this" << std::endl; } void DoThat( std::string message ) { std::cout << message << std::endl; } }; // Class for example 4: overloaded class members class Overload { p...

Getting started with the Boost libraries in Cygwin

Image
This post assumes that the boost libraries have been downloaded and extracted to the directory of your choice. See this previous posting for more details on how to download and extract the Boost libraries. Open Cygwin and cd to the location where your Boost libraries have been installed. For Windows directories with white spaces please enclose it in quotes, eg: $ cd "C:\Program Files\boost_1_46_1" Check to see that the boost/ folder lives at this location using $ ls : If so, run the bootstrap: $ ./bootstrap.sh –prefix=boost/ Then run the bjam executable (takes a while) $ ./bjam And run bjam installer (takes a very long while): $ ./bjam install After this has completed you will find that within the boost/ folder, three other folders have been created, namely bin , include and lib . Recursively copy these 3 folders to the /usr/local/ destination: $ cp -r boost/bin /usr/local/ $ cp -r boost/include /usr/local/ $ cp -r boost/lib /usr/local/ ...