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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16552754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:30:50  来源:igfitidea点击:

Compile 32 bit binary on 64 bit system

windowsgo

提问by pluralism

I've coded a Go program in a 64 bit system but I want to compile a 32 bit binary!

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

The 64 bit binary is working just great but I have no idea how to create a 32 bit binary.

64 位二进制文​​件运行良好,但我不知道如何创建 32 位二进制文​​件。

How can I do it?

我该怎么做?

采纳答案by alex

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:

如果您从源代码构建 Go,那么您可以为任何 CPU 和操作系统构建任何额外的编译器和库。如果您在 windows/amd64 上并且想要为 windows/386 构建,那么这将构建您需要为 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:

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

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=386before you invoke any commands for windows/386.

由于您使用的是 windows/amd64,您甚至应该能够运行/测试您的 windows/386 程序。只需确保set GOARCH=386在为 windows/386 调用任何命令之前。

One caveat: this does not support cgo, so you cannot use any packages that use cgo.

一个警告:这不支持cgo,因此您不能使用任何使用cgo.

回答by miltonb

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 32bitzip and unpack to a second folder:

我在 Windows 7 64 位 PC 上实现这一点的方法(没有任何编译器编译)首先安装了 windows amd64,然后下载 32 位zip 并解压到第二个文件夹:

\go\go32
\go\go64

By then adjusting the PATHand the GOROOTinside my command prompt window as follows:

届时调整PATHGOROOT我的命令提示符窗口中,如下所示:

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.

然后我回去重新编译我之前使用 64 位编译的应用程序。如果您想定期切换,所有这些都可以批量设置。

回答by pluralism

Ok, I've finally solved the problem! Here it is how I did it(I was miserably failing actualy!).

好的,我终于解决了这个问题!这就是我是如何做到的(我实际上失败了!)。

  • First thing I did was to download GCC from http://www.mingw.org/.
  • Next add 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 go to C:\Go\src and run all.bat from the command line.
  • Then you have to 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!
  • 我做的第一件事是从http://www.mingw.org/下载 GCC 。
  • 接下来将 C:\MinGW\bin 添加到 PATH 环境变量中(我假设 MinGW 安装在 C:\MinGw 中)

  • 在运行 go build/go install 之前的下一件事是设置环境变量。

  • 打开命令提示符并转到 C:\Go\src 并从命令行运行 all.bat。
  • 然后你必须将 GOOS、GOARCH 和 CGO_ENABLED 分别设置为 windows、386 和 0!(你还应该将 GOPATH 设置为当前 Go 项目所在的路径)。
  • 接下来运行 make.bat 和 make.bat --no-clean 之后你就可以为 32 位系统构建你的项目了!我希望这是有帮助的!

回答by Hui Tan

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

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

:: if "x%GOROOT_BOOTSTRAP%"=="x" set GOROOT_BOOTSTRAP=%HOMEDRIVE%%HOMEPATH%\Go1.4

Change GOROOT_BOOTSTRAP to my Go's root:

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

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

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

set GOOS AND GOARCH as follow

设置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

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

但我收到错误,这不是 32 位程序。