【问题标题】:Calling cgo macro function调用 cgo 宏函数
【发布时间】:2018-08-18 03:20:26
【问题描述】:

我有一个 cgo 程序:

package main

//#define sum(a,b) (a)+(b)
import "C"

func main() {
        print(C.sum(1,2))
}

应该很直接,打印3。但是编译失败:

could not determine kind of name for C.sum

作为我在 cgo 中找到的有限文档,经过一些测试,cgo 可以与宏常量一起使用,但我怎样才能让它与宏函数/参数一起使用?

【问题讨论】:

标签: go cgo


【解决方案1】:

很简单,你只需要一个包装器:

//#define SUM(a,b) (a)+(b)
//int sum(int a, int b) {
//  return SUM(a,b);
//}
import "C"

func main() {
    print(C.sum(1, 2))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-28
    • 2021-12-22
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多