Golang1.8编译静态库给C使用

来源:互联网 发布:mac搜索快捷键是什么 编辑:程序博客网 时间:2024/05/17 04:45

Go实例代码:

package mainimport (    "fmt")import "C"//export Printffunc Printf(format, str string) {    fmt.Printf(format, str)}func main(){}

编译命令:
go build -ldflags “-s -w” -buildmode=c-archive -o printf.a main.go

生成:printf.a printf.h两个文件

C代码实例:

#include "printf.h"void main(){    char fm[8]="Age:%s\n";    GoString format={fm,sizeof(fm)};    GoString values={"25",2};    Printf(format,values);}

编译命令:
linux 编译命令:gcc main.c printf.a -L. -o main -lpthread -s
windows:编译命令:gcc -o main.exe 1.c libprint.a -lwinmm -lstdc++ -lws2_32 -lntdll
生成:main main.exe

原创粉丝点击