buidmod为c-archive能在window下生成 ,c-shared则不行
1.golang生成c-archive静态库
main.go
package main import "C" func main() {} //export Hello
func Hello() string {
return "Hello"
} //export Test
func Test() {
println("export Test")
}
bd.bat
set GOARCH=amd64
set GOBIN=C:\gonew\go18\bin
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=E:\GOPROJECT\
set GORACE=
set GOROOT=C:\gonew\go18\
set GOTOOLDIR=C:\gonew\go18\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1 %GOBIN%/go build -x -v -ldflags "-s -w" -buildmode=c-archive -o main.a main.go
生成main.a 以及main.h
2.window下使用mingw64 gcc调用main.a
main.c
#include <stdio.h>
#include "main.h" void main()
{
GoString str;
str = Hello();
Test();
printf("%d\n",str.n);
}
bdc.bat
gcc -o main.exe main.c main.a -lwinmm -lstdc++ -lws2_32 -lntdll
生成main.exe