Search code examples
gocgo

How to cross compile Go to 32 bit windows .exe


I need to compile a set of Go + C code into both 64 bit and 32 bit exe under my 64 bit windows system. My compiler is TDM-GCC-64 9.2.0. The compilation was smooth for 64 bits and a .exe file was created.

But the compilation failed after I set

$Env:GOARCH = 386

to get a 32 bit exe file. The error msg was "main.go:4:2: build constraints exclude all Go files in C:\Users\xxx\test\src". The directory \src contains a test c function and go function to call c.

Please help. Thanks.


Solution

  • Eventually I figured out it is key to set up four relevant env variables properly, the 32 bit program can be compiled smoothly.

    $env:GOOS="windows"
    $env:GOARCH="386" 
    $env:CGO_ENABLED=1
    $env:GOGCCFLAGS="-m32 -mthreads -fmessage-length=0"
    go build -x -buildmode=c-shared -o xxx.so main.go