1、Go调用C:在go文件里调C(以下代码中除了开头的注释之外,其他注释不可删除)
/*
* go 和 C 互调用程序
*/
package main
/*
int Add( int a, int b ) {
return a + b;
}
*/
import "C"
import (
"fmt"
)
func main() {
fmt.Println(C.Add(1, 2))
}
上面的C代码虽然被“注释”了,但是Go可以直接调
2、Go调用C:通过.h头文件调(以下代码中除了开头的注释之外,其他注释不可删除)
/*
* go 和 C 互调用程序
*/
package main
/*
#include "MyHeadFile.h"
*/
import "C"
import (
"fmt"
)
func main() {
fmt.Println(C.MyFunc("Hello"))
}
上面代码以注释的方式导入MyHeadFile.h头文件,然后可以直接使用其中的函数
3、Go生成动态库dll(以下代码中除了开头的注释之外,其他注释不可删除)
/*
* Go生成动态库的命令:go build -o cgo.dll -