Here we demonstrate how to compile a Go library under Linux, but to be used by C source with gcc
on Windows.
Pull the official golang docker: (it's a Debian buster)
docker pull golang:1.14
In that container, install mingw w64 gcc:
apt install gcc-mingw-w64
This will produce two files: libaw.a
, libaw.h
.
GOOS=windows CC=x86_64-w64-mingw32-gcc CGO_ENABLED=1 go build -v -o libaw.a -buildmode=c-archive aw.go
This will produce two files: aw.dll
, aw.h
.
GOOS=windows CC=x86_64-w64-mingw32-gcc CGO_ENABLED=1 go build -v -o aw.dll -buildmode=c-shared aw.go
The compiler used is (from gcc --version
)
gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
This will produce a standalone portable c.exe
.
gcc -pthread -o c.exe client.c -L. -law
This will produce a c.exe
, which is portable along with aw.dll
.
gcc -pthread -o c.exe client.c -L. -law
Or we can cross-compile it on Linux like:
x86_64-w64-mingw32-gcc -pthread -o bin/client.exe client.c -Ibin -Lbin -law