Reputation: 1505
I've coded a Go program in a 64 bit system but I want to compile a 32 bit binary!
The 64 bit binary is working just great but I have no idea how to create a 32 bit binary.
How can I do it?
Upvotes: 35
Views: 41161
Reputation: 1505
Ok, I've finally solved the problem! Here it is how I did it(I was miserably failing actually!).
The first thing I did was to download GCC from http://www.mingw.org/.
Next I added C:\MinGW\bin to the PATH environment variable(I'm assuming that MinGW is installed in C:\MinGw)
Next thing before running go build/go install is to set the enviroment variables.
Open the command prompt and cd to C:\Go\src and run all.bat from the command line.
Set GOOS, GOARCH and CGO_ENABLED to windows, 386 and 0 respectively! (you should also set GOPATH to the path where your current Go project is).
Next run make.bat and make.bat --no-clean After that you can build your project for 32 bit systems! I hope this is helpful!
Upvotes: 2
Reputation: 103
My Go version is 1.14 ,I change make.bat as follow:
:: if "x%GOROOT_BOOTSTRAP%"=="x" set GOROOT_BOOTSTRAP=%HOMEDRIVE%%HOMEPATH%\Go1.4
Change GOROOT_BOOTSTRAP to my Go's root:
if "x%GOROOT_BOOTSTRAP%"=="x" set GOROOT_BOOTSTRAP=D:\Go
after that I got this error
go tool dist: unknown $goarch 386
then I set GOOS and GOARCH
setlocal
set GOROOT=%GOROOT_BOOTSTRAP%
set GOOS=
set GOARCH=
set GOBIN=
set GO111MODULE=off
"%GOROOT_BOOTSTRAP%\bin\go.exe" build -o cmd\dist\dist.exe .\cmd\dist
endlocal
set GOOS AND GOARCH as follow
setlocal
set GOROOT=%GOROOT_BOOTSTRAP%
set GOOS=windows
set GOARCH=386
set GOBIN=
set GO111MODULE=off
"%GOROOT_BOOTSTRAP%\bin\go.exe" build -o cmd\dist\dist.exe .\cmd\dist
endlocal
But I got error this is not a 32bit program.
Upvotes: 2
Reputation: 2248
If you built your Go from source, then you can build any additional compilers and libraries for any CPU and OS. If you are on windows/amd64 and want to build for windows/386, then this will build everything you need to compile for windows/386:
set GOARCH=386
cd %GOROOT%\src
make.bat --no-clean
Once you have done that, you can build your windows/386 executable with:
set GOARCH=386
cd %YOUR_PROG_DIR%
go build
Since you are on windows/amd64, you should be able to even run / test your windows/386 programs too. Just make sure to set GOARCH=386
before you invoke any commands for windows/386.
One caveat: this does not support cgo
, so you cannot use any packages that use cgo
.
Upvotes: 30
Reputation: 7355
The way I have achieved this (without any compiling of the compiler) on my Windows 7 64 bit PC was first having the windows amd64 installed, then download 32bit zip and unpack to a second folder:
\go\go32
\go\go64
By then adjusting the PATH
and the GOROOT
inside my command prompt window as follows:
set PATH=\go\go32;%PATH%
set GOROOT=\go\go32\
Then I went back and recompiled my app that was previously compiling using the 64 bit. All this can be setup in a batch if you want to switch between regularly.
Upvotes: 9