在64位系统上编译32位二进制文件。

huangapple go评论97阅读模式
英文:

Compile 32 bit binary on 64 bit system

问题

我在一个64位系统中编写了一个Go程序,但我想编译一个32位的二进制文件!

64位的二进制文件运行得很好,但我不知道如何创建一个32位的二进制文件。

我该如何做呢?

英文:

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?

答案1

得分: 28

如果您从源代码构建了Go,那么您可以为任何CPU和操作系统构建任何额外的编译器和库。如果您在windows/amd64上,并且想要为windows/386构建,那么这将构建您编译windows/386所需的一切:

<!-- language: lang-none -->

set GOARCH=386  
cd %GOROOT%\src  
make.bat --no-clean  

完成后,您可以使用以下命令构建您的windows/386可执行文件:

<!-- language: lang-none -->

set GOARCH=386  
cd %YOUR_PROG_DIR%  
go build  

由于您在windows/amd64上,您甚至可以运行/测试您的windows/386程序。只需确保在调用任何针对windows/386的命令之前设置set GOARCH=386

一个注意事项:这不支持cgo,因此您不能使用任何使用cgo的包。

英文:

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:

<!-- language: lang-none -->

set GOARCH=386  
cd %GOROOT%\src  
make.bat --no-clean  

Once you have done that, you can build your windows/386 executable with:

<!-- language: lang-none -->

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.

答案2

得分: 9

我实现这个的方式(没有编译编译器)在我的Windows 7 64位电脑上是首先安装了Windows amd64,然后下载32位压缩包并解压到第二个文件夹中:

\go\go32
\go\go64

然后在命令提示符窗口中调整PATHGOROOT

set PATH=\go\go32;%PATH%
set GOROOT=\go\go32\

然后我回去重新编译之前使用64位编译的应用程序。如果你想经常切换,所有这些都可以在批处理中设置。

英文:

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.

答案3

得分: 3

好的,我终于解决了这个问题!以下是我是如何解决的(实际上我一开始失败得很惨!)。

  • 首先,我从http://www.mingw.org/下载了GCC。

  • 接下来,我将C:\MinGW\bin添加到PATH环境变量中(我假设MinGW安装在C:\MinGw\中)。

  • 在运行go build/go install之前,需要设置环境变量。

  • 打开命令提示符,cd到C:\Go\src,并从命令行运行all.bat。

  • GOOSGOARCHCGO_ENABLED分别设置为windows3860(你还应该将GOPATH设置为当前Go项目的路径)。

  • 接下来运行make.bat和make.bat --no-clean。
    完成后,你就可以为32位系统构建你的项目了!希望这对你有帮助!

英文:

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!

答案4

得分: 2

我的Go版本是1.14,我将make.bat更改如下:

:: 如果“x%GOROOT_BOOTSTRAP%”==“x”则设置GOROOT_BOOTSTRAP=%HOMEDRIVE%%HOMEPATH%\Go1.4

将GOROOT_BOOTSTRAP更改为我的Go根目录:

如果“x%GOROOT_BOOTSTRAP%”==“x”则设置GOROOT_BOOTSTRAP=D:\Go

之后我遇到了这个错误:

go tool dist: unknown $goarch 386

然后我设置了GOOS和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

将GOOS和GOARCH设置如下:

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

但是我得到了错误,提示这不是一个32位程序。

英文:

My Go version is 1.14 ,I change make.bat as follow:

:: if &quot;x%GOROOT_BOOTSTRAP%&quot;==&quot;x&quot; set GOROOT_BOOTSTRAP=%HOMEDRIVE%%HOMEPATH%\Go1.4

Change GOROOT_BOOTSTRAP to my Go's root:

if &quot;x%GOROOT_BOOTSTRAP%&quot;==&quot;x&quot; 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
&quot;%GOROOT_BOOTSTRAP%\bin\go.exe&quot; 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
&quot;%GOROOT_BOOTSTRAP%\bin\go.exe&quot; build -o cmd\dist\dist.exe .\cmd\dist
endlocal

But I got error this is not a 32bit program.

huangapple
  • 本文由 发表于 2013年5月15日 04:59:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/16552754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定