SIGVerse with Python


If you questions after reading this page: Please feel free to email at jain@nii.ac.jp


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

This tutorial is independent of SIGVerse simulator and anyone who wants to embed a python interpreter inside C++ source code using Visual Studio can use this.

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. I will show how to parallize the compilation below which makes computation very fast ( ranging from 13 mins when 8 threads are used to 40 mins when 4 threads are used simultaneously). Without the use of multiple threads, 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 . 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.
    • 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:

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

  • 5. Strat cmd and type the following:

Note: X in above command, is the number of threads your PC can handle. Please go to My Computer(right click)-->Device Manager --> Processors to see the list of processors you have. Usually, the list of processors are equal to no. of threads that could be run concurrently. So, its 4 processors, please use -j4 option.

This will compile both static and shared libraries and install them to the specified path.

Please note that, if you use the option to parallelize the compilation available for gcc (-jX where X is the number of thread handled by the machine processor) then it will be faster, otherwise this operation takes a lot of time (more or less around one hour), a lot of memory and a lot of disk space. So, if you are not using the -jX option, then please make sure to close all unnecessary programs to avoid "out of memory" errors. I believe you can make this operation much faster by specifying only the required libraries. But for my test I installed all libraries to be on safer 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.
  • 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.

Counter: 6945, today: 2, yesterday: 0

Front page   Edit Freeze Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes
Last-modified: 2014-06-23 (Mon) 08:41:54 (3593d)