Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 751 Bytes

cgo_macro_func.md

File metadata and controls

29 lines (22 loc) · 751 Bytes

cgo 调用 C 中的宏函数

结论

cgo 目前只支持可以规约为常量或变量的宏,不支持宏函数。需要添加封装函数才能在 cgo 中使用。比如下面的代码

package main

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

func main() {
  // println(C.SUM(1, 2))  // error: could not determine kind of name for C.SUM
  println(C.sum(1, 2)) // ok
  // println(C.sum2(1, 2)) // error: could not determine kind of name for C.sum2
}

参考