Tuesday, September 24, 2013

Compiling boost and boost.python, running a hello world on Windows / Visual Studio C++ 2012

This tutorial guides you with the steps required to embed python code into c++ code using Microsoft Visual Studio 2012. Please note that this installs all the libs and headers supplied with boost, not just the ones that are necessary for boost.python.

1. Install Microsoft Visual Studio 2012. Apply all the patches to it.
2. Install Python. You can get it from here. I used version "Python 3.3.2 Windows x86 MSI Installer (Windows binary -- does not include source)" in this example. Add Python executable to PATH environment variable. In this example I used C:\Python33\ as the destination folder.
3. Download and un7zip boost. You can get it from here. I used version "boost_1_54_0.7z". In this example I used "C:\Program Files\boost\boost_1_54_0\" as the destination folder.
4. Add user-config.jam to your home directory. In my case it was C:\Users\gvb\. The contents of the user-config.jam is as follows:

import toolset : using ;
using python
     : 3.3
     : c:\\Python33\\python # cmd-or-prefix
     : # includes
     : # libraries
     ;

5. Start cmd. Type the following:

cd "C:\Program Files\boost\boost_1_54_0"

bootstrap

b2 link=static,shared threading=single,multi toolset=msvc-11.0 --libdir=C:\Boost\lib\i386 install

This will compile both static and shared libraries and install them to the specified path. Please note that this operation takes a lot of time (more or less around one hour), a lot of memory and a lot of disk space. Please make sure to close all unnecessary programs to avoid "out of memory" errors. I believe you can make this operation faster by specifying only the required libraries.

6. Start Visual Studio 2012. Choose "File-->New-->Project (Ctrl+Shift+N)"
Choose "Visual C++ --> Win32 Console Application". Press Next, deselect "Precompiled header".

Go to Property Manager ("View --> Other Windows --> Property Manager")
Expand "ConsoleApplication1 --> Debug | Win32"
Double-click on Microsoft.Cpp.Win32.User.
Expand "Common Properties --> C/C++ --> General".
In "Additional Include Directories" add these two entries:

C:\Python33\include
C:\Boost\include\boost-1_54

Expand "Common Properties --> Linker --> General".
In "Additional Library Directories" add these two entries:

C:\Python33\libs
C:\Boost\lib\i386

Expand "Common Properties --> Linker --> Input".

C:\Python33\libs\python33.lib
C:\Boost\lib\i386\boost_python-vc110-mt-gd-1_54.lib

Close the property sheets.



Remove all the existing contents of ConsoleApplication1.cpp, and add the following code:

#include <boost/python.hpp>
using namespace boost::python;

int main()
{
    try {
        Py_Initialize();

        object main_module((
            handle<>(borrowed(PyImport_AddModule("__main__")))));

        object main_namespace = main_module.attr("__dict__");

        handle<> ignored(( PyRun_String( "print (\"Hello, World\")",
            Py_file_input,
            main_namespace.ptr(),
            main_namespace.ptr() ) ));
    } catch( error_already_set ) {
        PyErr_Print();
    }
}

Build the application (F6).

In order to run the application, you need to copy "C:\Boost\lib\i386\boost_python-vc110-mt-gd-1_54.dll" to your output directory (in my case it is "C:\Projects\ConsoleApplication1\Debug\").

Run the application (F5).

You should see "Hello, World" text in the console output.

That's pretty much it.