1. 确保已在 Windows 上安装好 Golang 环境,并将其 bin 目录添加到系统环境变量中。

  2. 在命令行中执行以下命令安装交叉编译工具:

go get github.com/mitchellh/gox


3. 进入需要编译的项目目录,并执行以下命令进行交叉编译:

go build -o main_linux_amd64 -trimpath -ldflags '-s -w' -a -installsuffix cgo -tags 'netgo,osusergo' -o main_linux_amd64 main.go


其中 `-osarch` 参数指定了目标操作系统的类型和 CPU 架构。这里指定的是 Linux 平台的 AMD64 架构。

4. 编译完成后,在项目目录下会生成名为 `main_linux_amd64` 的可执行文件。

5. 将生成的 `main_linux_amd64` 可执行文件上传到 Linux 服务器上即可运行。
Windows 上 Golang 交叉编译 Linux 可执行文件

原文地址: https://www.cveoy.top/t/topic/jLdr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录