无法将 unsafe.Pointer(指向 C 函数的指针)作为函数调用。

huangapple go评论88阅读模式
英文:

Unable to Call unsafe.Pointer(Pointed to C Function) as Function

问题

我正在尝试调用具有相同参数的C函数,但在编译过程中出现了错误cannot call non-function f (type unsafe.Pointer)

现在我正在尝试使用go函数包装C函数,并将go函数添加到切片中,如下所示:

package main

/*
int add(int a, int b) {
  return a+b;
}
int sub(int a, int b) {
  return a-b;
}
*/
import "C"
import (
	"fmt"
)

func add(a, b int) int {
	return (int)(C.add(C.int(a), C.int(b)))
}

func sub(a, b int) int {
	return (int)(C.sub(C.int(a), C.int(b)))
}

func main() {
	fx := [](func(int, int) int){
		add, sub,
	}
	for _, f := range fx {
		fmt.Printf("Result: %d\n", f(1, 2))
	}
}

有没有办法从unsafe.Pointer调用C函数?我正在使用GO 1.8.1 Linux。

英文:

I'm trying to call C function with same signature they take 2 int arguments. and this error cannot call non-function f (type unsafe.Pointer) appear during compile.

package main

/*
int add(int a, int b) {
  return a+b;
}
int sub(int a, int b) {
  return a-b;
}
*/
import "C"
import (
	"fmt"
	"unsafe"
)

func main() {
	a := C.int(1)
	b := C.int(2)
	fx := make([]unsafe.Pointer, 2)
	fx[0] = C.add
	fx[1] = C.sub
	for _, f := range fx {
		fmt.Printf("Result: %d\n", f(a, b))
	}
}

Now I'm working around with wrap C function with go function and add go function into slice instead like this

package main

/*
int add(int a, int b) {
  return a+b;
}
int sub(int a, int b) {
  return a-b;
}
*/
import "C"
import (
	"fmt"
)

func add(a, b int) int {
	return (int)(C.add(C.int(a), C.int(b)))
}

func sub(a, b int) int {
	return (int)(C.sub(C.int(a), C.int(b)))
}

func main() {
	fx := [](func(int, int) int){
		add, sub,
	}
	for _, f := range fx {
		fmt.Printf("Result: %d\n", f(1, 2))
	}
}

Is there any way to call C function from unsafe.Pointer, I'm using GO 1.8.1 Linux

答案1

得分: 1

根据Go对C的引用,我可以解决我的问题。

> 目前不支持调用C函数指针,但是你可以声明Go变量来保存C函数指针,并在Go和C之间传递它们。C代码可以调用从Go接收到的函数指针。

看起来我需要创建一个桥接函数来调用C函数指针。

package main

/*
int add(int a, int b) {
  return a+b;
}
int sub(int a, int b) {
  return a-b;
}

typedef int(*math_fp)(int, int);

const int len_operator = 2;

math_fp math_operators[2] = {
	add, sub
};

int do_math(math_fp op, int a, int b) {
	return op(a, b);
}
*/
import "C"
import (
	"fmt"
)

func main() {
	var fnCnt C.int = 2
	var i C.int
	for i = 0; i < fnCnt; i++ {
		op := C.math_fp(C.math_operators[i])
		fmt.Printf("Result: %d\n", C.do_math(op, 1, 2))
	}
}

返回预期结果

$ go run src/test.go 
Result: 3  
Result: -1
英文:

I can solve my problem, according to Go references to C.

> Calling C function pointers is currently not supported, however you
> can declare Go variables which hold C function pointers and pass them
> back and forth between Go and C. C code may call function pointers
> received from Go.

It's looks like I have to create a bridge function for call C function pointer

package main

/*
int add(int a, int b) {
  return a+b;
}
int sub(int a, int b) {
  return a-b;
}

typedef int(*math_fp)(int, int);

const int len_operator = 2;

math_fp math_operators[2] = {
	add, sub
};

int do_math(math_fp op, int a, int b) {
	return op(a, b);
}
*/
import &quot;C&quot;
import (
	&quot;fmt&quot;
)

func main() {
	var fnCnt C.int = 2
	var i C.int
	for i = 0; i &lt; fnCnt; i++ {
		op := C.math_fp(C.math_operators[i])
		fmt.Printf(&quot;Result: %d\n&quot;, C.do_math(op, 1, 2))
	}
}

Return expected result

$ go run src/test.go 
Result: 3  
Result: -1

huangapple
  • 本文由 发表于 2017年5月17日 17:00:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/44020148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定