Python Boost: module import issues

I am currently trying to use Boost Python to export a class and then use it in the appropriate program.

/**
    main.cpp
*/
#define BOOST_PYTHON_STATIC_LIB
#include <Resource\ZipResourceFile.hpp>
#include <Resource\ResourceCache.hpp>
#include <Windows.h>
#include <boost/python.hpp>
#include <iostream>

/* a simple add method, for s & g */
int add(int a, int b)
{
    return a + b;
}

/* Foo class*/
class Foo
{
public:
    Foo(int n);
    ~Foo();
    void f();
};

/* Foo ctor, does nothingm just wanted to pass and arg */
Foo::Foo(int n)
{

}

/* destructor */
Foo::~Foo()
{
}

/* f() implementation, calls Foo!!! to cout */
void Foo::f()
{
    std::cout << "Foo!!!" << '\n';
}

/* Boost python module definition */
BOOST_PYTHON_MODULE(PyBackend)
{
    using namespace boost::python;

    def("add", add);
    class_<Foo>("Foo", init<int>())
        .def("f", &Foo::f);
}



int main(int argc, char* argv[])
{
    PyImport_AppendInittab("PyBackend", init_PyBackend);
    Py_Initialize();
    PyRun_SimpleString("import PyBackend");
    PyRun_SimpleString("foo = PyBackend.Foo(1)");

    Py_Finalize();

    {
        int n;
        std::cin >> n;
    }
    return 0;
}

Anyway, I have no idea where I can find the init_PyBackend function, although this seems like a logical thing that I would call if I hadn't used Boost.Python.

The module itself is not in a separate DLL; it compiled everything at the same time. Anyway, who has any ideas on what I can do?

+5
source share
1 answer

Naming convention for module initialization function:

  • init*** for Python 2.x (without underscore).
  • PyInit_*** for Python 3.x.

The Boost.Python macro BOOST_PYTHON_MODULEfollows these conventions.

Python 3.2, PyBackend :

PyInit_PyBackend.

, , , _sre, init init_sre/PyInit__sre ( Python 3.x).

+10

All Articles