golang[83]-golang-mac交叉编译windows程序

安装mingw:

1
$ brew install mingw-w64

编译x64

可执行

1
CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ GOOS=windows GOARCH=amd64 go build -x -v -ldflags "-s -w" -o test_x64.exe

静态库

1
$ CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ GOOS=windows GOARCH=amd64 go build -buildmode=c-archive -x -v -ldflags "-s -w" -o bin/x64/x64.a main.go

动态库

将-buildmode=c-archive改为-buildmode=c-shared即可

编译x86

1
$ CGO_ENABLED=1 CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ GOOS=windows GOARCH=386 go build -x -v -ldflags "-s -w" -o test_x86.exe

静态库

1
$ CGO_ENABLED=1 CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ GOOS=windows GOARCH=386 go build -buildmode=c-archive -x -v -ldflags "-s -w" -o bin/x86/x86.a main.go

动态库

将-buildmode=c-archive改为-buildmode=c-shared即可

参考资料

https://stackoverflow.com/questions/36915134/go-golang-cross-compile-from-mac-to-windows-fatal-error-windows-h-file-not-f