当使用CGO库的release版本时出现编译错误,解决方法是设置CGO_ENABLED环境变量为1,同时在使用CGO库时,需要在命令中加上“-ldflags '-s -w'”参数。示例代码如下:
// main.go package main
//#include "hello.h" import "C"
func main() { C.hello() }
// hello.h #ifndef HELLO_H #define HELLO_H
void hello();
#endif
// hello.c
#include
void hello() { printf("Hello, world!\n"); }
// 编译命令 CGO_ENABLED=1 go build -ldflags '-s -w'