5

I am using swig to link c++ with go, but I want to use go functions in my c++ code. I have previously used cgo, and know something like this would work:

//bind.h
extern void GoFunc(*C.char);
void CFunc();
//////////////////


//bind.cc
void CFunc() {
  //do stuff
}
//////////////////


//main.go
package main

//#include "bind.h"
import "C"

//export GoFunc
func GoFunc(*C.char) {
  //do go stuff
}

func main() {
  C.CFunc()
}
//////////////////

but when I try to replicate this in swig,

//bind/bind.i
%module bind
%{
  extern void Foo();
  #include "bar.hpp"
%}

%include <typemaps.i>
%include "bar.hpp"
///////////////////////////


//bind/bar.hpp
class Bar {
  Bar() {}
  void Call() {
    Foo();
  }
};
///////////////////////////


//main.go
package main

import "fmt"
import "test/bind"

//export Foo
func Foo() {
  fmt.Println("FOO WAS CALLED")
}

func main() {
  bar := bind.NewBar()
  bar.Call()
}
///////////////////////////

I get the following compiler error:

In function `_wrap_Foo_bind_2f8d0a395235e7eb':
bind/bind_wrap.cxx:274: undefined reference to `Foo()'
collect2.exe: error: ld returned 1 exit status

why does this code work in cgo, but not in swig, and how can i implement the same feature in swig?

Ank i zle
  • 2,089
  • 3
  • 14
  • 36

0 Answers0