Home >Backend Development >Golang >How to Cross-Compile Go Programs from Windows to Linux?

How to Cross-Compile Go Programs from Windows to Linux?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-27 05:28:13787browse

How to Cross-Compile Go Programs from Windows to Linux?

Cross Compiling from Windows to Linux

Envision running a program compiled on a Windows machine flawlessly on a Linux system. Cross-compiling makes this scenario a reality. However, certain challenges arise when attempting to cross-compile from Windows to Linux.

Problem: Attempting to compile a program with "go build" while specifying a Linux target on Windows results in an error: "runtime: linux/amd64 must be bootstrapped using make.bat".

Answer: This error indicates a lack of necessary tools for cross-compilation. To resolve this issue, follow these steps:

  1. Ensure that your Windows GOARCH is set to "amd64".
  2. Execute the following batch script to build the required tools:

    set GOARCH=amd64
    set GOOS=linux
    go tool dist install -v pkg/runtime
    go install -v -a std

Note: If your Windows GOARCH is "386", you will need to download mingw gcc and follow the instructions provided by user2714852.

For additional guidance, refer to the tutorial available at https://golang.org/wiki/WindowsCrossCompiling for instructions on cross-compiling from Linux.

The above is the detailed content of How to Cross-Compile Go Programs from Windows to Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn