go语言在linux下交叉编译带图标的windows可执行程序。

使用GoVersionInfo来添加exe图标(弃用,查看后文改进方法

项目地址:https://github.com/josephspurrier/goversioninfo/

安装

go get github.com/josephspurrier/goversioninfo/cmd/goversioninfo

配置

1、在main文件的第一行添加如下代码

 //go:generate goversioninfo -icon=testdata/resource/icon.ico -manifest=testdata/resource/goversioninfo.exe.manifest

其中 -icon代表图标的路径,-manifest指定manifest文件路径

2、添加文件

将图标ico文件和manifest文件放到上一步指定的路径下,并将 versioninfo.json 文件放到main.go同级目录下。 goversioninfo.exe.manifest文件内容如下,文件名也可以自定义

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
   <assemblyIdentity
     type="win32"
     name="Github.com.JosephSpurrier.GoVersionInfo"
     version="1.0.0.0"
     processorArchitecture="*"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="asInvoker"
          uiAccess="false"/>
        </requestedPrivileges>
    </security>
  </trustInfo>
 ​

versioninfo.json文件内容如下:

 {
     "FixedFileInfo": {
         "FileVersion": {
             "Major": 1,
             "Minor": 0,
             "Patch": 0,
             "Build": 0
         },
         "ProductVersion": {
             "Major": 1,
             "Minor": 0,
             "Patch": 0,
             "Build": 0
         },
         "FileFlagsMask": "3f",
         "FileFlags ": "00",
         "FileOS": "040004",
         "FileType": "01",
         "FileSubType": "00"
     },
     "StringFileInfo": {
         "Comments": "",
         "CompanyName": "",
         "FileDescription": "",
         "FileVersion": "",
         "InternalName": "",
         "LegalCopyright": "",
         "LegalTrademarks": "",
         "OriginalFilename": "",
         "PrivateBuild": "",
         "ProductName": "",
         "ProductVersion": "v1.0.0.0",
         "SpecialBuild": ""
     },
     "VarFileInfo": {
         "Translation": {
             "LangID": "0409",
             "CharsetID": "04B0"
         }
     },
     "IconPath": "",
     "ManifestPath": ""
 }

将里面的字段“ProductVersion”更改为你想要的版本号即可.(同时也可以修改其他信息如:版权,产品名称,文件说明等)

3、mod初始化

在main.go所在的目录执行 go mod init 命令,会生成 go.mod 文件

4、编译

首先用 go generate 命令生成resource.syso资源文件,然后进行交叉编译exe文件

 CGO_ENABLED=0 GOOS=windows GOARCH=386 go build ./

注意这里需要用Directory的方式编译,不能用File的方式编译。(也就是不能用go build main.go 这种方式)。原因是通过Directory的方式编译,编译器会自动寻找同目录下的资源文件并打包进EXE。

改进

查看GoVersionInfo文档发现,其核心功能(生成*.syso文件)使用一个叫rsrc的工具来实现的 https://github.com/akavel/rsrc 使用此工具不必修改源码

1、安装rsrc

go get github.com/akavel/rsrc

2、使用

   -arch string
         architecture of output file - one of: 386, amd64, [EXPERIMENTAL: arm, arm64] (default "amd64")
   -ico string
         comma-separated list of paths to .ico files to embed
   -manifest string
         path to a Windows manifest file to embed
   -o string
         name of output COFF (.res or .syso) file; if set to empty, will default to 'rsrc_windows_{arch}.syso'

可以使用 -ico来指定图标 示例: rsrc -arch 386 -ico main.ico -o windows.syso windows.syso名字可以自定义,文件扩展名不变,执行完成生成一个windows.syso文件。

3、编译

 CGO_ENABLED=0 GOOS=windows GOARCH=386 go build ./

代码调用生成

package main

import (
	"fmt"
	"os/exec"
)

func buildExeWithIco(sourcePath string, icoPath string, exePath string) bool {
	fmt.Println(sourcePath, icoPath)
	cmd := "cd " + sourcePath + "; " + "rsrc -arch 386 -ico " + icoPath + " -o windows.syso && " + "CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -o " + exePath + " ./"
	fmt.Println(cmd)
	out, err := exec.Command("bash", "-c", cmd).Output()
	fmt.Println(string(out))
	if err != nil {
		fmt.Printf("Failed to execute command: %s", cmd)
		return false
	}
	return true
}

func main() {
	buildExeWithIco("/home/code/example/main", "/home/code/example/main.ico", "/home/code/example/test.exe")
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值