无法将 unsafe.Pointer(指向 C 函数)作为函数调用
Unable to Call unsafe.Pointer(Pointed to C Function) as Function
我正在尝试调用具有相同签名的 C 函数,它们采用 2 个 int 参数。这个错误 cannot call non-function f (type unsafe.Pointer)
出现在编译过程中。
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))
}
}
现在我正在使用 go 函数包装 C 函数并将 go 函数添加到 slice 中,而不是像这样
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
我可以根据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.
看来我必须创建一个 bridge function
来调用 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))
}
}
Return 预期结果
$ go run src/test.go
Result: 3
Result: -1
我正在尝试调用具有相同签名的 C 函数,它们采用 2 个 int 参数。这个错误 cannot call non-function f (type unsafe.Pointer)
出现在编译过程中。
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))
}
}
现在我正在使用 go 函数包装 C 函数并将 go 函数添加到 slice 中,而不是像这样
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
我可以根据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.
看来我必须创建一个 bridge function
来调用 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))
}
}
Return 预期结果
$ go run src/test.go
Result: 3
Result: -1