Saturday, July 19, 2014

HelloWorld with Boost, Visual Studio 2013 and Python Tools for Visual Studio

So I've Boost compiled and prepared for some serious things and big projects :).
But first I was eager to test of wrapping some simple C++ code with Boost library and calling the resulting library from Python and all of that within one instance of Visual Studio 2013.
This is a result of my effort on experimental HelloWorld library incorporating:
  1. Python (libs and includes being referenced from Visual Studio)
  2. Visual Studio 2013
  3. Python Tools for Visual Studio
  4. Boost 1.5.5(compiled and lib and includes directories being referenced from Visual Studio)
So here is my really stupid simple sample of HelloWorld that is based on slightly changed code available in Boost documentation (see snippet below).
The main things for library to be callable from Python that are really important and are not aforementioned in Boost documentation:
  1. Resulting DLL has to be renamed to PYD. Python interpreted does not load DLL (At least I was not successful in convincing an interpreter to do that)
  2. Resulting PYD library has to have the same name like parameter of BOOST_PYTHON_ MODULE(...). So in my case I have hello as a parameter so correspondingly my compiled module is named hello.pyd.
  3. PYD file has to be available/findable for Python interpreter. Verify that your file is in corresponding %PYTHONPATH%. Otherwise Python's import hello (in my case) would cause runtime errors.
#define BOOST_ALL_DYN_LINK
#define BOOST_LIB_DIAGNOSTIC
#define BOOST_PYTHON_STATIC_LIB
#include <boost/python.hpp>
using namespace boost::python;

class World
{
public:
 void set(std::string msg) { this->msg = msg; }
 std::string greet() { return msg; }
 std::string msg;
 friend std::ostream &operator<<(std::ostream &o, World const &w) { o << w.msg ; return o; }
};


BOOST_PYTHON_MODULE(hello)
{
 class_<World>("World")
  .def("greet", &World::greet)
  .def("set", &World::set)
  .def(self_ns::str(self_ns::self))
  ;
}

The sample code is available on github:

Once I have had library compiled I was test it within Visual Studio's Python interpreter. (I really like the simplicity of  combining C++ and Python in one development tool). That's really cool.





Friday, July 18, 2014

Compiling Boost with Visual Studio 2013 for 64bits

Recently I needed to use Boost library in one of my project. So I've downloaded Boost package from boost.org and needed to build the whole Boost package for my Visual Studio 2013
The required steps I've had to do to build the Boost package were following.
  1. Open command prompt in root of %boost_install_dir%
  2. Execute bootstrap.bat
  3. Execute b2 toolset=msvc-12.0 --build-type=complete --libdir=%mylibs%\lib\x64 architecture=x86 address-model=64 install -j4 
  4. Add %mylibs%\lib\x64 into the libs path in the Visual Studio
Mission accomplished.

Update 2014-12-05:
Do not expect that building 32bit boost against 64bit version of python will bring any promising result :(.

Tuesday, July 8, 2014

Install python package (pandas) fails on Windows 7 64bit


UPDATE 2014/11/11: New tool Microsoft Visual C++ Compiler for Python 2.7 available. Not tested yet, but seems to solve the problem....

Recently I was experiencing following problem when I've needed to install some python package. 

File "{%python%}\lib\distutils\msvc9compiler.py", line 299, in query_vcvarsall
raise ValueError(str(list(result.keys())))
ValueError: [u'path']


I've had installed VS 2008 Express edition already. After some investigation I've realized that VS2008 Express does not have support for x64. So I've had to made following few steps to make python package installer running smoothly:

1. Download corresponding iso image from Windows SDK for Windows 7 and .NET Framework 3.5 SP1 and install it
2. Locate vcvars64.bit in %INSTALL_FOLDER%Microsoft Visual Studio 9.0\VC\bin\
3. Copy vcvars64.bat to .\vcvarsamd64.bat
4. Copy vcvars64.bat to .\amd64\vcvarsamd64.bat

After these steps I was able to install required package successfully.