0

I currently have a source files written in C++ and wrapped into a Python module (uses Boost and Parser libs).

I ported the C++ folder under go/src and .so files along with main.go

Program Structure

src
   /main
      main.go
      network.so
   /network
       file1.cpp (this has a function **object DBdata::getTable())
       file1.hpp (#define FILE1_H_)
   

main.go

package main

// #cgo pkg-config: python3
// #cgo CFLAGS : -I./ -I/usr/include/python3.6
// #cgo LDFLAGS: -L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/usr/lib -lpython3.6 -lpthread -ldl  -lutil -lm
// #include <Python.h>
import "C"

import "fmt"

func main() {

  cmd := exec.Command("python", "-c", "import network; network.getTable()")
  cmd.Dir = "/home/username/go/src/network"    //directory where is my python code
  out,err := cmd.CombinedOutput()
} 

After building this main.go, I get the error as

/usr/include/boost/config/no_tr1/memory.hpp: fatal error: memory: No such file or directory 
>  #  include 
>             ^~~~~~~~
> compilation terminated.

How to import .so as Python modules in Go? Can Swig be used in this place?

What is the better approach to expose Python module to Go?

Amelia
  • 41
  • 5
  • Are you trying to compile your C++ with a C compiler? – user2357112 Mar 17 '21 at 16:21
  • @user2357112supportsMonica, I used g++ – Amelia Mar 17 '21 at 16:30
  • So, one of the `*.[hc]pp` files has the `#include ` directive, is it? – kostix Mar 17 '21 at 16:41
  • I checked the existing c++ source file. No sign of memory header files – Amelia Mar 17 '21 at 16:45
  • 2
    One more thing: your `main.go` merely runs `python` interpreter's _process_ and tells it to load the module `network` then call a function provided by that module, and the whole thing here is to collect the output of that `python` process. I hence fail to see why are you trying to use [`cgo`](https://golang.org/cmd/cgo/) and/or swig as the task appears to be completely unrelated to calling code form a CPython extension from Go; your Go code does nothing more than calling an external process. Why not just build the CPython module "the normal way" and then make sure the python process finds it? – kostix Mar 17 '21 at 16:46
  • OK, let's add more context to that `fatal error:` message. Could you please update your question with the full output produced by the whatever thing you has called, and which has failed. – kostix Mar 17 '21 at 16:48
  • I just edited my post giving more visibility to the error – Amelia Mar 17 '21 at 16:51
  • BTW is this the contunuation of [a previous attempt](https://stackoverflow.com/q/66489991)? – kostix Mar 17 '21 at 16:52
  • Yes. I missed adding the entire error. I edited my post to show the entire error message upon building the Go file – Amelia Mar 17 '21 at 16:56
  • @kostix “ Why not just build the CPython module "the normal way" and then make sure the python process finds it?” —- how to implement this ? – Amelia Mar 17 '21 at 16:58
  • @Amelia, I would ask whoever made the module. If that was you, you should supposedly already known the answer. Also: do you really need to build the module? Your question begins with mentioning `network.so` is already present, and to me, it looks like a dynamically-loadable library ("shared object", hence the extension `.so`) for a Unix-like system. – kostix Mar 17 '21 at 17:29
  • I added #include , rebuild the code and the message I see now is File "", line 1, in ModuleNotFoundError: No module named 'network'. – Amelia Mar 18 '21 at 01:28

1 Answers1

0

Issue fixed. The .so placed in /bin and the Go was build and could access the functions under network

Amelia
  • 41
  • 5