使用方式

具体参数,使用gf build -h查看帮助

仅限于交叉编译使用到GoFrame框架的项目,支持绝大部分常见系统的直接交叉编译。

内置编译变量

build命令自动嵌入编译变量,这些变量用户可自定义,并且在运行时通过gbuild组件获取。使用gf build项目将会默认嵌入以下变量(参考gf -v):

  • 当前Go版本
  • 当前GoFrame版本
  • 当前Git Commit(如果存在)
  • 当前编译时间

编译配置文件

build支持同时从命令行以及配置文件指定编译参数、选项。GoFrame框架的所有组件及所有生态项目都是使用的同一个配置管理组件,默认的配置文件以及配置使用请参考章节 配置管理。以下是一个简单的配置示例供参考:

gfcli:
  build:
    name:     "gf"
    arch:     "all"
    system:   "all"
    mod:      "none"
    packSrc:  "resource,manifest"
    version:  "v1.0.0"
    output:   "./bin"
    extra:    ""

配置选项的释义同命令行同名选项。

名称默认值含义示例
name与程序入口go文件同名生成的可执行文件名称。如果是windows平台,那么默认会加上.exe后缀gf
arch当前系统架构编译架构,多个以,号分隔,如果是all表示编译所有支持架构386,amd64,arm
system当前系统平台编译平台,多个以,号分隔,如果是all表示编译所有支持平台linux,darwin,windows
path./bin编译可执行文件存储的目录地址./bin
mod
go build -mod编译选项,不常用none
cgofalse是否开启CGO,默认是关闭的。如果开启,那么交叉编译可能会有问题。
packSrc
需要打包的目录,多个以,号分隔,生成到internal/packed/build_pack_data.gopublic,template,manifest
packDst

internal/packed/build_pack_data.go

打包后生成的Go文件路径,一般使用相对路径指定到本项目目录中
version
程序版本,如果指定版本信息,那么程序生成的路径中会多一层以版本名称的目录v1.0.0
output
输出的可执行文件路径,当该参数指定时,namepath参数失效,常用于编译单个可执行文件。./bin/gf.exe
extra
额外自定义的编译参数,会直接传递给go build命令
varMap
自定义的内置变量键值对,构建的二进制中可以通过gbuild包获取编译信息。
gfcli:
  build:
    name:     "gf"
    arch:     "all"
    system:   "all"
    mod:      "none"
    cgo:      0
    varMap:
      k1: v1
      k2: v2
exitWhenErrorfalse当编译发生错误时,立即停止后续执行,并退出编译流程(使用os.Exit(1)
dumpEnvfalse每次编译之前在终端打印当前编译环境的环境变量信息

编译时的内置变量可以在运行时通过gbuild构建信息-gbuild 获取。

使用示例

$ gf build
2020-12-31 00:35:25.562 start building...
2020-12-31 00:35:25.562 go build -o ./bin/darwin_amd64/gf main.go
2020-12-31 00:35:28.381 go build -o ./bin/freebsd_386/gf main.go
2020-12-31 00:35:30.650 go build -o ./bin/freebsd_amd64/gf main.go
2020-12-31 00:35:32.957 go build -o ./bin/freebsd_arm/gf main.go
2020-12-31 00:35:35.824 go build -o ./bin/linux_386/gf main.go
2020-12-31 00:35:38.082 go build -o ./bin/linux_amd64/gf main.go
2020-12-31 00:35:41.076 go build -o ./bin/linux_arm/gf main.go
2020-12-31 00:35:44.369 go build -o ./bin/linux_arm64/gf main.go
2020-12-31 00:35:47.352 go build -o ./bin/linux_ppc64/gf main.go
2020-12-31 00:35:50.293 go build -o ./bin/linux_ppc64le/gf main.go
2020-12-31 00:35:53.166 go build -o ./bin/linux_mips/gf main.go
2020-12-31 00:35:55.840 go build -o ./bin/linux_mipsle/gf main.go
2020-12-31 00:35:58.423 go build -o ./bin/linux_mips64/gf main.go
2020-12-31 00:36:01.062 go build -o ./bin/linux_mips64le/gf main.go
2020-12-31 00:36:03.502 go build -o ./bin/netbsd_386/gf main.go
2020-12-31 00:36:06.280 go build -o ./bin/netbsd_amd64/gf main.go
2020-12-31 00:36:09.332 go build -o ./bin/netbsd_arm/gf main.go
2020-12-31 00:36:11.811 go build -o ./bin/openbsd_386/gf main.go
2020-12-31 00:36:14.140 go build -o ./bin/openbsd_amd64/gf main.go
2020-12-31 00:36:17.859 go build -o ./bin/openbsd_arm/gf main.go
2020-12-31 00:36:20.327 go build -o ./bin/windows_386/gf.exe main.go
2020-12-31 00:36:22.994 go build -o ./bin/windows_amd64/gf.exe main.go
2020-12-31 00:36:25.795 done!



Content Menu

  • No labels

47 Comments

  1. -ldflags="-s -w" 这样的参数可以加吗

    1. 是支持的,但是不建议加,编译后的二进制减少不了多少容量,这样会去掉一些符号和编译信息。

      1. 好的。谢谢。

      2. fy

        怎么加两个参数?比如-ldflags="-s -w"和-trimpath,双引号需要用"\"吗?

        1. gf build main.go -a amd64 -s linux n main -e "-trimpath -ldflags '-w -s'"

  2. 2.0.0 版本  build 出现如下信息: 

    PS C:\Users\Desktop\Codes\001_lc\LC-XQH-Go-Test> gf build --gf.gcfg.file=./_tmp/build.toml -v None
    2021-12-28 15:54:24.154 [FATA] Error: exit status 128 
    PS C:\Users\Desktop\Codes\001_lc\LC-XQH-Go-Test> gf build --gf.gcfg.file=./_tmp/build.toml        
    2021-12-28 15:54:27.977 [FATA] Error: exit status 128

    PS C:\Users\刘爽\Desktop\Codes\001_lc\LC-XQH-Go-Test> gf build
    2021-12-28 15:54:49.041 [FATA] Error: exit status 128
    PS C:\Users\刘爽\Desktop\Codes\001_lc\LC-XQH-Go-Test> gf build main.go
    2021-12-28 15:55:05.031 [FATA] Error: exit status 128 


    测试平台是 windows 11

    配置文件为:


    # gf 命令行参数
    [gfcli]
    [gfcli.build]
    name = "lc-test-tool"
    system = "linux"
    arch = "arm"
    mod = "none"
    cgo = 0
    path = "./_out"


    main.go 就是简单的 hello world 程序



    1. 解决了吗 我也遇到了

      1. 换了老版本.

    2. 2022-01-23已修复,重新下载gf安装再试~

  3. gf build
    2021-12-28 23:28:44.219 [FATA] Error: exit status 128

  4. 请问,我的项目有多个入口文件需要编译,我应该怎么配置

    1. 走不通的入口文件

  5. 在mac中明明已经关闭CGO了,但还是报以下错误

    `2022-02-27 04:39:14.498 failed to build, os:linux, arch:amd64, error:
    # runtime/cgo
    linux_syscall.c:67:13: error: implicit declaration of function 'setresgid' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    linux_syscall.c:67:13: note: did you mean 'setregid'?
    `


    以下我的配置文件:
    `

    gfcli:
    build:
    name: "gf-MT"
    arch: "amd64"
    system: "linux"
    mod: "none"
    cgo: 0
    pack: "manifest,resource"
    version: ""
    output: "./bin"
    extra: ""

    `

    1. 当我直接使用go build的时候,可以正常编译,但使用gf 就会出现以上报错

      1. 使用最新版本,然后gf build xxx --debug=true看看调试输出的语句是什么?

        1. 解决了,是因为通过gf-cli配置文件设置cgo关闭不管用,无论设置什么,都为true,然后我重编译了一下gf-cli工具,把Cgo所在的结构体tag的orphan:设为false就可以了

      2. 配置文件中不需指定关闭cgo,golang中boolean类型默认false,gf-cli中的cgo带有标签`orphan:"true"`,如果指定此选项,那么无论指定的是0或者1都为true,不知道这样理解对不对

  6. 在mac中直接使用 CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go  命令编译是可以在linux上运行的,但是端口不是我在config中定义的端口,于是想使用gf  build 但是抛出以下错误

    错误信息
    2022-04-13 11:15:22.783 failed to build, os:linux, arch:amd64, error:
    # runtime/cgo
    linux_syscall.c:67:13: error: implicit declaration of function 'setresgid' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    linux_syscall.c:67:13: note: did you mean 'setregid'?
    /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/unistd.h:593:6: note: 'setregid' declared here
    linux_syscall.c:73:13: error: implicit declaration of function 'setresuid' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    linux_syscall.c:73:13: note: did you mean 'setreuid'?
    /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/unistd.h:595:6: note: 'setreuid' declared here
    
    

    以下是我的配置:

    gfcli:
      build:
        name:      "school-grid-report"
        arch:      "amd64"
        system:    "linux"
        mod:       "none"
        cgo:       0
        pack:      "manifest/config"
        version:   "0.01"
        output:    "./bin/school-grid-report"
        extra:     ""
    1. 已解决,用的楼上的方法,我把gf卸载了,下载源码包把gf-cli中的cgo带有标签`orphan:"true" 改为false

  7. 郭强 gf build得到的二进制文件运行时,报错

    root@xview:~# ./mygf-app
    resource/i18n does not exist
    1. resource/i18n does not exist
       1).  github.com/gogf/gf/v2/i18n/gi18n.(*Manager).SetPath
        	/root/go/pkg/mod/github.com/gogf/gf/v2@v2.0.7-0.20220505141935-ad04adcceaaa/i18n/gi18n/gi18n_manager.go:101
       2).  myapp/internal/cmd.glob..func1
        	/root/go-workspace/MyGoFrame/myapp/internal/cmd/cmd.go:29
       3).  github.com/gogf/gf/v2/os/gcmd.(*Command).doRun
        	/root/go/pkg/mod/github.com/gogf/gf/v2@v2.0.7-0.20220505141935-ad04adcceaaa/os/gcmd/gcmd_command_run.go:117
       4).  github.com/gogf/gf/v2/os/gcmd.(*Command).RunWithValueError
        	/root/go/pkg/mod/github.com/gogf/gf/v2@v2.0.7-0.20220505141935-ad04adcceaaa/os/gcmd/gcmd_command_run.go:68
       5).  github.com/gogf/gf/v2/os/gcmd.(*Command).RunWithValue
        	/root/go/pkg/mod/github.com/gogf/gf/v2@v2.0.7-0.20220505141935-ad04adcceaaa/os/gcmd/gcmd_command_run.go:32
       6).  github.com/gogf/gf/v2/os/gcmd.(*Command).Run
        	/root/go/pkg/mod/github.com/gogf/gf/v2@v2.0.7-0.20220505141935-ad04adcceaaa/os/gcmd/gcmd_command_run.go:26
       7).  main.main
        	/root/go-workspace/MyGoFrame/myapp/main.go:12

    resource/i18n是我的i18n文件,另外还有一些静态文件放在 resource/public.

    我的配置文件是:

    [gfcli]
        [gfcli.build]
            name=      "mygf-app"
            path=      "./bin"
            arch=      "amd64"
            system=    "linux"
            mod=       "none"
            cgo=       0
            pack=      "manifest/config,resource/i18n,resource/public"
            version=   ""
            output=    ""
            extra=     ""

    并且我在main.go文件已经引入了pack

    package main
    
    import (
    	"myapp/internal/cmd"
    	_ "myapp/packed"
    
    	"github.com/gogf/gf/v2/os/gctx"
    )
    
    // 入口调用gf cmd
    func main() {
    	cmd.Main.Run(gctx.New())
    }


    我的gf版本是:

    GoFrame CLI Tool v2.0.6, https://goframe.org 
    GoFrame Version: v2.0.7-0.20220505141935-ad04adcceaaa in current go.mod 
    CLI Installed At: /usr/local/lib/go/bin/gf 
    CLI Built Detail:
      Go Version:  go1.17.7
      GF Version:  v2.0.6
      Git Commit:  2022-03-31 16:57:32 66803fd6641e31c60f0489180668243ea092b87b
      Build Time:  2022-03-31 16:55:38

    是不是gf出了bug ?

    1. 大佬这个问题搞定了吗,我也遇到这个问题

      1. 的确是gf的bug。 build出来的二进制文件没有包含 pack指定的目录。gf build 生成linux二进制文件没有打包resource和config · Issue #1799 · gogf/gf (github.com)  我提了issue,最新版本还没有尝试,你可以再试下

        1. 最新的2.1版本,pack改成了packSrc和packDst,但是仍然有问题

          1. 可以在github上提issue。 

            1. 早就提过了

                1. 这个issue就是我提的,demozx就是我

                  1. 谢谢大佬,gf build编译后,资源文件一直无法加载问题困扰了我好几天,终于解决了。特意来感谢

  8. 使用gf build编译linux端的包,可以正常运行,但运行后的服务端口是随机的,而不是配置中的8199,在Windows上正常。编译成exe, 在Windows上运行,通过g.Cfg().Get(ctx, "server")可以读取到配置,编译到linux平台读取不到,是空字符串的输出

    gfcli版本:GoFrame CLI Tool v2.1.0-rc4  (3月份的2.0.x版本也不行)

    gf版本: 2.0.6

    配置:

    server:
    address: ":8199"
    openapiPath: "/api.json"
    swaggerPath: "/swagger"

    logger:
    level: "all"
    stdout: true

    gfcli:
    # 编译设置
    build:
    name: "gf"
    arch: "amd64"
    system: "linux" #windows
    path: "./bin"


    1. 原来是我部署到linux上没有放配置文件,只需要把2.0项目里面的manifest文件夹整个放到gf同目录就行了,配置文件就在里面

  9. 能否编译成arm64+windows?

  10. $ gf build main.go
    2022-07-28 10:56:00.916 start building...
    2022-07-28 10:56:00.916 go build -o main main.go
    2022-07-28 10:56:02.712 failed to build, os:ios, arch:amd64, error:
    no required module provides package main.go; to add it:
            go get main.go

    you may use command option "--debug" to enable debug info and check the details


  11. 这文档参数一会儿pack一会儿packSrc,坑死人

  12. 我的项目有多个入口文件,使用gf build的时候报[FATA] Error: exit status 128 ,boot下面是有多个文件夹,对应多个入口。

    多个项目是共用service,logic,api,model这些的,boot下面的每个文件夹,都有对应的main.go,以及cmd和controller

    gf build ./boot/admin/main.go

    这个地方该如何处理哦??


  13. 我下载了示例工程focus-single,使用gf build也是同样报错,go run main.go是正常的,

    PS E:\work\saner\focus-single> go version      
    go version go1.19.2 windows/amd64
    PS E:\work\saner\focus-single> gf -v
    GoFrame CLI Tool v2.0.0, https://goframe.org 
    GoFrame Version: v2.2.1 in current go.mod 
    CLI Installed At: D:\gopath\bin\gf.exe
    Current is a custom installed version, no installation information.
    PS E:\work\saner\focus-single>

  14. 最新版本编译是把指定配置文件的参数删掉了吗


  15. 建议增加每次编译后的运行前置/后置命令行

    比如配置个 cmd:"md5sum {file} >> {file}.md5" 其中{file为编译文件}

    这样的话可以让我每次编译后生成对应的md5

    或者执行upx等操作

  16. 2.4.4后又不能交叉编译了,编译了后配置文件老师引不进来,之前都不是这样的

  17. 谁知道用这个怎么提升win系统的UAC权限

  18. 2.6.4交叉编译了,配置文件引不进来,一直报错

    TOTAL FILES: 0 这个也没扫描到

    1. 找不到配置文件,检查自己的配置信息,项目目录下是否存在`hack/config.yaml`

        1. # CLI.
          gfcli:
          gen:
          dao:
          - link: "mysql:gfast3:gfast333@tcp(127.0.0.1:3306)/gfast-v32"
          tables: "sys_role_dept"
          removePrefix: "gf_"
          descriptionTag: true
          noModelComment: true
          path: "./internal/app/system"
          build:
          name: "main"
          arch: "amd64"
          system: "linux"
          mod: "none"
          packSrc: "manifest,resource"
          version: "v1.0.0"
          path: "./bin"
          extra: ""


        2. 执行的命令是什么,在哪个目录执行?

          1.  找到问题了,gfast里 main.go 里面少引用了 _ "internal/packed" 导致的