Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile 32 bit binary on 64 bit system

Tags:

windows

go

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?

like image 529
pluralism Avatar asked May 14 '13 20:05

pluralism


People also ask

Can a 64-bit system run a 32bit program?

Can I run 32-bit programs on a 64-bit computer? Most programs made for the 32-bit version of Windows will work on the 64-bit version of Windows except for most Antivirus programs. Device drivers that are made for the 32-bit version of Windows will not work correctly on a computer running a 64-bit version of Windows.

Can I run Ubuntu 32-bit on a 64-bit computer?

All 64-bit (x86-64) CPUs should be fast enough to run Ubuntu and can run the 32-bit (x86) version as well.

Is gcc 32-bit compiler?

Mostly compiler(gcc or clang) of C and C++, nowadays come with default 64-bit version.

Is gcc 32 or 64-bit?

Compile 32-bit program on 64-bit gcc in C and C++Nowadays the compiler comes with default 64-bit version. Sometimes we need to compile and execute a code into some 32bit system. In that time, we have to use thisS feature. At first, we Shave to check the current target version of the gcc compiler.


2 Answers

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.

like image 163
alex Avatar answered Oct 19 '22 23:10

alex


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.

like image 36
miltonb Avatar answered Oct 19 '22 23:10

miltonb