1,c文件
#include <stdio.h>
#include <stdlib.h>
extern int sun(int a, int b) {
return a + b;
}
2,go调用示例
package main
// #include "c_fun.h"
import "C"
import "fmt"
func main() {
fmt.Println("go call c: 3+4=", C.sun(3, 4))
}
3,说明
输出:go call c: 3+4= 7
c_fun.h是标准的C,声明一个sun函数。在go文件中,#include要放在注释里; 还有import "C",要单独成一行。
本文介绍了一个简单的Go语言调用C语言函数的例子。通过示例代码展示了如何在Go中使用C函数,并强调了在Go中正确使用C头文件的方法。
1884

被折叠的 条评论
为什么被折叠?



