python , Boost and straight (but complex) C code
Sebastian 'lunar' Wiesner
basti.wiesner at gmx.net
Sat Dec 30 08:16:50 EST 2006
Osiris <nono at hotmail.com> typed
> I have these pieces of C-code (NOT C++ !!) I want to call from Python.
> I found Boost.
> I have MS Visual Studio 2005 with C++.
>
> is this the idea:
> I write the following C source file:
> ============================
> #include <iostream>
iostream is a C++ header file...
> #include <stdafx.h>
>
> namespace { // Avoid cluttering the global namespace.
C doesn't know about namespaces. They are a C++ thing, too.
> int my_int; /* a global integer: or outside namespace ? */
> double calc ( double f)
> {
> my_int = (int) (f/2);
> printf( "Half of %f is %d\n", f, my_int );
You include a C++ IO header, but use traditional C IO functions here.
Either you use C++ streams here, or you replace <iostream> with
<stdio.h>.
> return f/2;
> }
>
> }
>
> #include <boost/python.hpp>
> using namespace boost::python;
>
> BOOST_PYTHON_MODULE( half )
> {
> def("calc", calc );
> }
>
> ================================
>
> Which I put in a VC project and compile into a .DLL
> This DLL I put somewhere on my disk, where Python 2.4 can find it.
> then I write the following Python source:
> =====================
> from half import *
> calc(34655.0)
>
> et voila ?
>
> Can I acces my_int too, this way ?
May be... Didn't you try it?
It may also be, that you have to create a PyObject first...
--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
More information about the Python-list
mailing list