0
votes

I was following https://www.boost.org/doc/libs/1_74_0/libs/python/doc/html/tutorial/tutorial/exposing.html. But the boost tutorial doesn't say how to build it. It recommends using b2 so I followed the instruction in the previous page of the link above. (I had added b2 in the $PATH and made a symbolic link for libboost_python.so under /usr/local/lib). I once compiled and ran this world.cpp example (see exposing C++ class in Python ( only ET_DYN and ET_EXEC can be loaded)) but this time it doesn't work somehow and now I'm trying to do it with b2.
Here is the world.cpp file.

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

struct World
{
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};

BOOST_PYTHON_MODULE(world_ext)
{
    using namespace boost::python;
    class_<World>("World")
        .def("greet", &World::greet)
        .def("set", &World::set)
    ;
}

and here is the Jamfile.

# Copyright Stefan Seefeld 2016.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

import python ;

project tutorial
  : requirements
    <location>.
    ;
# ------ for hello
#python-extension hello_ext : hello.cpp ;
#
#run-test hello : hello_ext hello.py ;
#
#alias test : hello ;
#explicit test ;

# ------ for world
python-extension world_ext : world.cpp ;

run-test world : world_ext world.py ;

alias test : world ;
explicit test ;

when I run b2, it gives me the error message below.(EDIT I fixed world to world_ext in the BOOST_PYTHON_MODULE() line and added using namespace boost::python; in the above code.)

...found 11 targets...
...updating 4 targets...
gcc.compile.c++ world.o
world.cpp: In function 'void init_module_world_ext()':
world.cpp:20:17: error: wrong number of template arguments (1, should be 4)
     class_<World>("World")
                 ^
In file included from /usr/local/include/boost/python/object_core.hpp:20:0,
                 from /usr/local/include/boost/python/args.hpp:22,
                 from /usr/local/include/boost/python/make_function.hpp:11,
                 from /usr/local/include/boost/python/def.hpp:11,
                 from world.cpp:8:
/usr/local/include/boost/python/def_visitor.hpp:14:56: note: provided for 'template<class T, class X1, class X2, class X3> class boost::python::class_'
 template <class T, class X1, class X2, class X3> class class_;
                                                        ^

    "g++"   -fPIC -O0 -fno-inline -Wall -g    -I"/usr/include/python3.5" -c -o "world.o" "world.cpp"

...failed gcc.compile.c++ world.o...
...skipped <p.>world_ext.so for lack of <p.>world.o...
...skipped <p.>world for lack of <p.>world_ext.so...
...failed updating 1 target...
...skipped 3 targets...

If I comment out the world portion and uncommenthello portion in the Jamfile, b2 works ok and I can run hello.py. The hello.cpp, hello.py, world.cpp, world.py files are all under the same directory. (~/BOOST/boost_1_73_0/libs/python/example/tutorial)

It looks like it about class exposing. It needes 4 template parameters but the original tutorial code gives just one. What should I do? (by the way, I hope the boost people update the tutorials for current boost version with more kind explanation. there are many discrepancies between documents and released code.)

1

1 Answers

0
votes

Python modules can be also built using the most popular Makefile or cmake.

From the command line that would be just:

g++ -I /usr/include/python2.7 -fpic -c -o main.o main.cpp
g++ -o world_ext.so -shared main.o -lboost_python -lpython2.7