Boost logo

Boost Users :

From: Keith MacDonald (boost_at_[hidden])
Date: 2005-04-21 09:27:40


I've tried to merge the Boost.Python tutorials for embedding and extending
(see below), to end up with an executable that does both. The script fails
to import "hello", so BOOST_PYTHON_MODULE is presumably not the correct way
to do it. What is?

Thanks,
Keith MacDonald

====== The C++ code ======
#include <boost/python.hpp>

using namespace boost::python;

char const* greet()
{
    return "hello, embedded world";
}

BOOST_PYTHON_MODULE(hello)
{
    def("greet", greet);
}

int main()
{
    Py_Initialize();

    object main_module((
        handle<>(borrowed(PyImport_AddModule("__main__")))));

    object main_namespace = main_module.attr("__dict__");

    try {
        handle<> ignored((PyRun_String(
            "import hello\n"
            "print hello.greet()\n"
          , Py_file_input
          , main_namespace.ptr()
          , main_namespace.ptr())
        ));
    }
    catch(error_already_set)
    {
        PyErr_Print();
    }

    Py_Finalize();
    return 0;
}

====== The Jamfile ======
subproject libs/python/example/tutorial/embedded ; # location of the program

# bring in the rules for python
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
include python.jam ;

exe hello # name of the executable
  : #sources
  hello.cpp
  : # requirements
  <find-library>boost_python <library-path>c:\boost\libs\python
  $(PYTHON_PROPERTIES)
 <library-path>$(PYTHON_LIB_PATH)
 <find-library>$(PYTHON_EMBEDDED_LIBRARY) ;


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net