Golang1.8编译静态库给C使用
Go实例代码:
package main
import (
"fmt"
)
import "C"
//export Printf
func Printf(format, str string) {
fmt.Printf(format, str)
}
func main(){}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
编译命令:
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
编译命令:
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