我正在使用 swig 将 c++ 与 go 链接起来,但我想在我的 c++ 代码中使用 go 函数。我以前使用过 cgo,并且知道这样的东西会起作用:
//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()
}
//////////////////
Run Code Online (Sandbox Code Playgroud)
但当我尝试在痛饮中复制这一点时,
//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()
}
///////////////////////////
Run Code Online (Sandbox Code Playgroud)
我收到以下编译器错误:
In function `_wrap_Foo_bind_2f8d0a395235e7eb':
bind/bind_wrap.cxx:274: undefined reference to `Foo()'
collect2.exe: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
为什么这段代码在 cgo 中有效,但在 swig 中无效,我如何在 swig 中实现相同的功能?