[[Tutorial]]
[[SIGVerse with Python]]

The aim of this tutorial is to guide you with the steps required to embed python code into c++ code using Microsoft Visual Studio 2010. 

Please note that:

-This installs all the libs and headers supplied with boost, not just the ones that are necessary for boost.python.
-Even if you are using x64 bit architecture, you don't have to install 64 bit Python and 64 bit compiler for Visual studio.  Even if you use 32 bit Python and 32 bit Visual Studio C++ compiler, it will work fine. This tutorial was tested on both platforms. 
-This tutorials uses boost from source, you may find pre-compiled binaries somewhere. But, I recommend using from source. But it is very time consuming (takes some 2-3 hours to install).

Okay, here are the steps.

-1. Install Microsoft Visual Studio 2008. Apply all the patches to it.
-2. Install Python. You can get it from [[here:http://www.python.org/getit/]] . I used version "Python 2.7.3 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:\Python27\ as the destination folder.
--We also tested with Python 3.3.2. It works fine. 

-3. Download and unzip boost. You can get it from [[here:http://www.boost.org/users/download/]]. 
--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. Placing  user-config.jam in your %HOMEDRIVE%%HOMEPATH% location, and configured Python inside it, is very crucial step.
--In my case it was C:\Users\(myname)\. The contents of the user-config.jam is as follows:

#highlight(c){{

using msvc : 9.0 ;
import toolset : using ;
using python
     : 2.7
     : C:\\Python27\\python # cmd-or-prefix
     : #C:\\Python27\\include
     : #C:\\Python27\\libs
     ;

}}

Please modify the contents according to your version of Visual Studio and Python.

-5. Strat cmd and type the following:

#highlight(c){{

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

bootstrap

b2 link=static,shared threading=single,multi toolset=msvc-9.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. But for my test I installed all libraries to be on safe side. 


-6. Start Visual Studio 2008. Choose "File-->New-->Project (Ctrl+Shift+N)"
Choose "Visual C++ --> General--> Empty Project". Please name the project. Lets call it "Test".  

In menu, go to Project-->Properties( Alt+F7 ).  

Expand "Common Properties --> C/C++ --> General".
In "Additional Include Directories" add these two entries:

     C:\Python27\include;
     C:\Boost\include\boost-1_54


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


     C:\Python27\libs;
     C:\Boost\lib\i386

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

     C:\Python27\libs\python27.lib
     C:\Boost\lib\i386\boost_python-vc90-mt-gd-1_54.lib


--7. Go to "Solution Explorer"-->Source-->C++ File. Please name the file. Lets call it testCode.cpp and paste the following code.

#highlight(c){{

#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();
    }
}

}}


--8. Build the application (F7).

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

Run the application (F5).

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

Congratulations, you have successful installed boost.python, configured it with Visual C++ and embedded python interpreter inside C++ code of visual studio. 

The next step is to create a Sigservice with embedded python interpreter.
I will show it in next tutorial.



#highlight(end)

#counter


Front page   New List of pages Search Recent changes   Help   RSS of recent changes