[C++-sig] Boost::Python for C code -> Segmentation fault

Mario Palomo mario-p at iname.com
Thu Jul 22 20:27:55 CEST 2004

I'd like to use Boost::Python to wrap C code in python, but my first test 
doesn't work:

FILE: hello.cpp
=================== BEGIN =======================
/*---------------- C code -------------------- */
#ifdef __cplusplus
extern "C" {

#include <stdio.h>

typedef struct user {
   char *name;
   int number;

void greet(user *u)
    printf("Hello %s, your number is %d.\n"
           , u->name, u->number);

#ifdef __cplusplus
/*-------------------------------------------- */

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

     .def_readwrite("name",   &user::name)
     .def_readwrite("number", &user::number)

   def("greet", greet);
==================== END ========================

I'm using Linux (Debian Sid) Boost::Python packages.
When I compile, I get:

$ g++ -c hello.cpp -Wall -I/usr/include/python2.3
$ ld -shared -o hello.so hello.o -lboost_python

All right, now I have 'hello.so', but when I try to use
the new module, I get a "Segmentation fault":

$ python
Python 2.3.4 (#2, Jul  5 2004, 09:15:05)
[GCC 3.3.4 (Debian 1:3.3.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import hello
 >>> u = hello.user()
 >>> u.number=10
Segmentation fault

What's wrong?

Thanks in advance

More information about the Cplusplus-sig mailing list