douyingbei1458
2017-05-17 09:00
浏览 184
已采纳

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

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
", 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
", f(1, 2))
    }
}

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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用具有2个int参数的相同签名来调用C函数。 并且在编译过程中出现此错误不能调用非函数f(类型unsafe.Pointer)

 包main 
 
 / * 
int  add(int a,int b){
返回a + b; 
} 
int sub(int a,int b){
返回ab; 
} 
 * / 
import“ C” 
import(  
“ fmt” 
“不安全” 
)
 
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(“结果:%d 
  “,f(a,b))
} 
} 
   
 
 

现在,我正在使用带有go函数的wrap C函数并将go函数添加到 像这样切片

 包main 
 
 / * 
int add(int a,int b){
 return a + b; 
} 
int sub  (int a,int b){
返回ab; 
} 
 * / 
import“ C” 
import(
“ fmt” 
)
 
func add(a,b int)int {\  n 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){
添加 ,sub,
} 
表示_,f:=范围fx {
 fmt.Printf(“结果:%d 
”,f(1,2))
} 
} 
   
 
 

有什么方法可以从 unsafe.Pointer 调用C函数,我正在使用GO 1.8.1 Linux

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsg41888 2017-05-18 08:31
    已采纳

    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 "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
    ", C.do_math(op, 1, 2))
        }
    }
    

    Return expected result

    $ go run src/test.go 
    Result: 3  
    Result: -1
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题