Calling Shared Library functions from Go and Python

  .so, boost-python, c++, go, python

Similar questions here
How can I call linux shared library functions in Go?

but too a beginner to understand it.

I am a C programmer and learning Go in a hard way.
Currently studying and porting an existing Python codes (python1.py and python3.py) to Go

The existing Python files(along with delphi.cpp) have been compiled and generated .so file
I need to access the functions under delphi.cpp (a C++ with boost python) from Go.

Python1.py calls the class Net defined under delphi.cpp

python1.py

class NetworkData(object):
 @classmethod
    def _init(cls):
     
 @classmethod
    def Func1(cls, keys): 

self.uname = hdd
self.__delphi = delphi.Net(hdd, python3.getAllData, NetworkData, kwargs)

 def TCP(self):
      
        return self.__delphi.TCP

python3.py

class getAllData(object):

    def _init(cls):
     
    ids = {
    "M1":          0x000002,
    "M2":        0x50001,
    }

delphi.cpp

#include <boost/python/module.hpp>
#include <stdio.h>
#define __PYTHON3__
using namespace boost::python;
using std::string;

BOOST_PYTHON_MODULE(delphi)
{
    PyEval_InitThreads();
    to_python_converter<beint32_t, beint32_to_python>();
    to_python_converter<beint64_t, beint64_to_python>();
    docstring_options doc_options;
    doc_options.disable_py_signatures();
    class_<Net>("Net", netDocs, init<string, object, object, dict>())
            .add_property("TCP",                &Net::getTCP,               tcpDocs)

}


static const char * netDocs =
        Net::Net(string hd, object getDataFn, object _networkdata, dict kwargs)

static const char * tcpDocs =
        std::string Net::getTCP() {
    return <data>();
}
 

I copied the .so under these locations.

Program structure

/go    
   /bin
      main
      delphi.so    
   /src
      /dependencies
         d1.go
         d2.go
      /main
        delphi.so
        main.go

delphi.cpp (uses Boost.Python). I need to import functions(eg TCP) defined under delphi.cpp

Before doing it in Go, I wanted to test the Python function from command prompt.
The subfunctions under delphi.Net are TCP, getType, callT(, UDS .

$python3
>>import delphi
>>delphi.Net.
>>delphi.Net.TCP
delphi.Net.getType
delphi.Net.callT(
delphi.Net.UDS

>>delphi.Net().TCP

I try calling delphi.Net().TCP and the error I got was

Boost.Python.ArgumentError: Python argument types in
Net.init(Net) did not match C++ signature:
init(_object*, std::__cxx11::basic_string<char, std::char_traits, std::allocator >,
boost::python::api::object, boost::python::api::object,
boost::python::dict)

I know I have called the TCP function without passing parameters and I don’t understand how to send the parameters to delphi.Net

delphi.Net(hdd, python3.getAllData, NetworkData, kwargs)

I tried my best to put forth the issue in an understandable way.

How to debug and understand such error?

I will be calling the same in Go using

exec.Command("python", "-c", "import delphi; delphi.Net().TCP)")

I wanted to test this portion from Python and then apply in Go.

Source: Windows Questions C++

LEAVE A COMMENT