a question about boost.python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Li Daobing

    #1

    a question about boost.python

    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<<(ostr eam& os, Rational r){
    return os;
    }
    BOOST_PYTHON_MO DULE(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_D YNAMIC_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=cont ents,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('Rati onal', ['Rational.cpp'],
    define_macros=[('BOOST_PYTHON_ DYNAMIC_LIB',
    None)],
    libraries=['boost_python'])]


    setup(name="itc c",
    version="0.2.2" ,
    author='Li Daobing',
    author_email='l idaobing@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_D YNAMIC_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_acc ess::visit(cons t V&, classT&)
    [with V =
    boost::python:: def_visitor<boo st::python::api ::object>, classT =
    boost::python:: class_<Rational ,
    boost::python:: detail::not_spe cified,
    boost::python:: detail::not_spe cified,
    boost::python:: detail::not_spe cified>]
    ':
    /usr/include/boost/python/def_visitor.hpp :67: instantiated from `void
    boost::python:: def_visitor<Der ivedVisitor>::v isit(classT&) const [with
    classT = boost::python:: class_<Rational ,
    boost::python:: detail::not_spe cified,
    boost::python:: detail::not_spe cified,
    boost::python:: detail::not_spe cified>, 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<Der ived>&) [with Derived =
    boost::python:: api::object, W = Rational, X1 =
    boost::python:: detail::not_spe cified, X2 =
    boost::python:: detail::not_spe cified, X3 =
    boost::python:: detail::not_spe cified]'
    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::v isit(boost::pyt hon::class_<Rat ional,
    boost::python:: detail::not_spe cified,
    boost::python:: detail::not_spe cified,
    boost::python:: detail::not_spe cified>&) const'
    make: *** [Rational.o] Error 1

Working...