a question about boost.python

Li Daobing lidaobing at gmail.com
Thu Jan 27 08:05:44 EST 2005


I can't use .def(str(self))
I write a simple example, without `str', I can build it well, but with
this one, I can't build

//Rational.cpp
#include <boost/python.hpp>
#include <iostream>

using namespace std;
using namespace boost::python;

class Rational
{};

ostream& operator<<(ostream& os, Rational r){
return os;
}
BOOST_PYTHON_MODULE(Rational)
{
class_<Rational>("Rational")
.def(str(self))                     // __str__
;
}
// end.

I don't know how to write Jamfile, so I write a Makefile, it works if i
don't use .def(str(self))

# Makefile
CC = g++

CFLAGS = -Wall -W -fPIC -I/usr/include/boost \
-I/usr/include/python2.3 -DBOOST_PYTHON_DYNAMIC_LIB \
-O2

LDFLAGS = -L/usr/local/lib -lboost_python -L/usr/lib/python2.3 \
-Wl,-rpath-link,.  -fPIC


CXXFLAGS = $(CFLAGS)

SLIB = hello.so Rational.so

all: $(SLIB)

%.so : %.o
/usr/bin/objcopy --set-section-flags .debug_str=contents,debug
$^
$(CC) $(LDFLAGS) $^ -shared -o $@

clean :
rm -f $(WORLD) $(OBJS)
# end.

or a simple setup.py, it also works if I don't use `str'

# setup.py
from distutils.core import setup, Extension

ext_modules = [Extension('Rational', ['Rational.cpp'],
define_macros=[('BOOST_PYTHON_DYNAMIC_LIB',
None)],
libraries=['boost_python'])]


setup(name="itcc",
version="0.2.2",
author='Li Daobing',
author_email='lidaobing at gmail.com',
ext_modules = ext_modules
)
# end.

This is the error message:
$ make
g++ -Wall -W -fPIC -I/usr/include/boost -I/usr/include/python2.3
-DBOOST_PYTHON_DYNAMIC_LIB -O2   -c -o Rational.o Rational.cpp
Rational.cpp: In function `std::ostream& operator<<(std::ostream&,
Rational)':
Rational.cpp:10: warning: unused parameter `Rational r'
/usr/include/boost/python/def_visitor.hpp: In static member function
`static
void boost::python::def_visitor_access::visit(const V&, classT&)
[with V =
boost::python::def_visitor<boost::python::api::object>, classT =
boost::python::class_<Rational,
boost::python::detail::not_specified,
boost::python::detail::not_specified,
boost::python::detail::not_specified>]
':
/usr/include/boost/python/def_visitor.hpp:67:   instantiated from `void
boost::python::def_visitor<DerivedVisitor>::visit(classT&) const [with
classT = boost::python::class_<Rational,
boost::python::detail::not_specified,
boost::python::detail::not_specified,
boost::python::detail::not_specified>, DerivedVisitor =
boost::python::api::object]'
/usr/include/boost/python/class.hpp:225:   instantiated from
`boost::python::class_<T, X1, X2, X3>& boost::python::class_<T, X1, X2,
X3>::def(const boost::python::def_visitor<Derived>&) [with Derived =
boost::python::api::object, W = Rational, X1 =
boost::python::detail::not_specified, X2 =
boost::python::detail::not_specified, X3 =
boost::python::detail::not_specified]'
Rational.cpp:15:   instantiated from here
/usr/include/boost/python/def_visitor.hpp:31: error: no matching
function for
call to
`boost::python::api::object::visit(boost::python::class_<Rational,
boost::python::detail::not_specified,
boost::python::detail::not_specified,
boost::python::detail::not_specified>&) const'
make: *** [Rational.o] Error 1




More information about the Python-list mailing list