0

This might have already been posted somewhere else but I couldn't find anything that worked. I have the following libraries / tools installed :

boost:
boost-python36.x86_64
boost-python36-devel.x86_64
boost-python36-static.x86_64

Python API:
python3-devel.x86_64

Here's my node.h file:

#pragma once

#include <python3.6m/Python.h>
#include <boost/python.hpp>
#include <boost/python/make_constructor.hpp>
#include <boost/python/detail/api_placeholder.hpp>

using namespace std;
using namespace boost::python;
class Node{
public:
    Node(PyObject* attr);
    ~Node();
    PyObject* _attr;
    static int number_of_nodes;
    int id;
    // other functions
};

node.cpp:

#include "node.h"
using namespace std;
//using namespace boost::python;

int Node::number_of_nodes = 0;
Node::Node(PyObject* attr){
    this->id = number_of_nodes;
    number_of_id++;
    this->_attr = attr;
}

Node::~Node(){

}
// other functions
BOOST_PYTHON_MODULE(Node){
    class_<Node>("Node", init<PyObject*>())
    // other functions
}

Makefile:

node.o:node.h node.cpp
    g++ -std=c++11 -fPIC -I/usr/include/python3.6m node.cpp -c -lpython3.6m -lboost_python3
Node.so:node.o
    g++ -std=c++11 -L /lib64 -shared node.o -o Node.so -lpython3.6m -lboost_python3

So far it works : no warnings, no compile errors. The .so file gets compiled without any issues.

the python file :

import Node # Node.so

When I try to run it, I get this error:

dynamic module does not define init function (initNode)

I checked online and apparently this can be caused by the name of the .so file not matching with the name of the module. However I made sure that the name is Node and the .so file is Node.so. Is there something I'm missing? I didn't see an explicit "initClassName" in any of the boost module I saw online.

Thanks

qwerty_99
  • 640
  • 5
  • 20
  • See [this answer](https://stackoverflow.com/a/37049403/4657412) specifically - you've built the module for Python 3 but are somehow expecting it to work on Python 2 – DavidW Jul 07 '20 at 17:06
  • didn't know that python2 was the default interpreter. Thanks! – qwerty_99 Jul 07 '20 at 18:38
  • 1
    FYI Python 2 expects a function called `initNode`, while Python 3 expects one called `PyInit_Node`. (Boost should handle this automatically for you depending on what version of Python you build it with) – DavidW Jul 07 '20 at 19:21

0 Answers0