调用 cgo 宏函数
Posted
技术标签:
【中文标题】调用 cgo 宏函数【英文标题】:Calling cgo macro function 【发布时间】: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 可以与宏常量一起使用,但我怎样才能让它与宏函数/参数一起使用?
【问题讨论】:
***.com/questions/44241836/c-macro-definitions-in-cgo(看起来你不能在 go 中使用'function'宏) 【参考方案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))
【讨论】:
以上是关于调用 cgo 宏函数的主要内容,如果未能解决你的问题,请参考以下文章