V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
blankmiss
V2EX  ›  Go 编程语言

求助 github action 自动打包动态链接库

  •  
  •   blankmiss · 337 天前 · 1092 次点击
    这是一个创建于 337 天前的主题,其中的信息可能已经有所发展或是发生改变。

    帮一个开源作者写的,我自己是不懂 golang 的

    name: Build
    on:
      release:
        types: [created] # 表示在创建新的 Release 时触发
    permissions:
      contents: write
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
        - name: Install Go 1.20.4
          uses: actions/setup-go@v2
          with:
            go-version: 1.20.4
        - uses: actions/checkout@v2
        - name: Install dependencies
          run: sudo apt-get update && sudo apt-get install -y build-essential
        - name: Build shared library for amd64
          run: cd libs_dist && chmod +x * && bash build_amd64_so.sh
        - name: Upload artifacts
          uses: actions/upload-artifact@v2
          with:
            name: libs
            path: |
              libs_dist/*.so
    
    

    理论上不是说 golang 是支持交叉编译的嘛

    # build arm64 so
    go env -w GOOS=linux
    go env -w GOARCH=arm64
    go build -buildmode=c-shared -o requests-go-arm64.so export.go
    

    我在 github 免费的 coderspace 里面测试了一下 amd64 是可以正常构建的,但是 arm 的就没办法 执行会提示

    @dnslin ➜ /workspaces/requests/libs_dist (main) $ go env -w GOOS=linux
    @dnslin ➜ /workspaces/requests/libs_dist (main) $ go env -w GOARCH=arm64
    @dnslin ➜ /workspaces/requests/libs_dist (main) $ go build -buildmode=c-shared -o requests-go-arm64.so export.go
    go: no Go source files
    

    如果不支持 一个平台构建其他平台的动态链接库 那我就不用测试了

    9 条回复    2023-05-23 19:19:08 +08:00
    gam2046
        1
    gam2046  
       337 天前   ❤️ 1
    看起来并不是一个纯 Go 的代码?如果用到了外部的,非 Go 的部分,也就是用到了 CGO 是不能直接这样编译的。需要你准备对应平台的(你的情况下,也就是 arm ) C/C++工具链,然后配置 CC/CXX 变量。
    blankmiss
        2
    blankmiss  
    OP
       337 天前
    @gam2046 https://github.com/wangluozhe/requests 这个项目 纯 golang 的
    yleoer
        3
    yleoer  
       337 天前   ❤️ 1
    我在 ubuntu 上尝试了下,就算是纯 Go 的代码,编译成动态链接库也需要 CGO ,就像 1 楼说的,我写下我成功编译出 arm 的动态链路库的过程:

    先 `apt install -y gcc-aarch64-linux-gnu` 下载 arm 的工具链,编译命令:`GOOS=linux && GOARCH=arm64 && GOARM=7 && CGO_ENABLED=1 && CC=arm-linux-gnueabihf-gcc && CXX=arm-linux-gnueabihf-g++ && AR=arm-linux-gnueabihf-ar&& go build -buildmode=c-shared -o requests-go-arm64.so export.go`
    virusdefender
        4
    virusdefender  
       337 天前   ❤️ 2
    export.go 里面是 cgo 的代码,不支持跨平台编译
    flyqie
        5
    flyqie  
       337 天前 via Android
    @yleoer #3

    c 调 go 的话本身在 go 编译的时候就需要 cgo 来处理。

    除 纯 go 代码编译独立二进制 以外的所有情况似乎都需要 cgo 。
    blankmiss
        6
    blankmiss  
    OP
       337 天前
    @yleoer 我测试了 还是会提示 go: no Go source files
    gam2046
        7
    gam2046  
       337 天前
    哦豁,是 export 到动态库的呀,那 CGO 是没跑了。

    这是我自己用的交叉编译的 Dockerfile

    https://gist.github.com/Lua12138/87b6ebb7c8cbc8e704a0cea4cba855a8

    你可以根据自己的情况替换路径,取消注释。

    然后把代码挂进去 CGO_ENABLED=1 ... go build 这样编译就行了
    sardina
        8
    sardina  
       337 天前 via iPhone
    你编译到动态库就必须要 cgo 了
    blankmiss
        9
    blankmiss  
    OP
       337 天前
    @gam2046 ths
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3188 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:35 · PVG 08:35 · LAX 17:35 · JFK 20:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.