GitHub Actions编译Rust无二进制文件生成问题排查
GitHub Actions交叉编译Rust程序无二进制文件问题排查
在使用GitHub Actions交叉编译Rust程序时,动作执行成功且目标目录已生成文件,但未产生预期的二进制文件。本地编译正常,仅指定交叉编译目标时出现该问题。
工作流文件
name: Compile and save program on: push: branches: [main] paths-ignore: ["samples/**", "**.md"] workflow_dispatch: jobs: build: strategy: fail-fast: false matrix: target: - aarch64-unknown-linux-gnu - i686-pc-windows-gnu - i686-unknown-linux-gnu - x86_64-pc-windows-gnu - x86_64-unknown-linux-gnu name: Build executable runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v3 - name: Set up Rust uses: actions-rs/toolchain@v1 with: toolchain: stable - name: Install dependencies run: | rustup target add ${{ matrix.target }} - name: Build uses: actions-rs/cargo@v1 with: use-cross: true command: build args: --target ${{ matrix.target }} --release --all-features --target-dir=/tmp - name: Debug missing files run: | echo "target dir:" ls -a /tmp/release echo "deps:" ls -a /tmp/release/deps - name: Archive production artifacts uses: actions/upload-artifact@v3 with: name: ${{ matrix.target }} path: | /tmp/release
目标目录结构(以Windows x86_64为例)
. ├── .cargo-lock ├── .fingerprint/ │ ├── libc-d2565b572b77baea/ │ ├── winapi-619d3257e8f28792/ │ └── winapi-x86_64-pc-windows-gnu-7e7040207fbb5417/ ├── build/ │ ├── libc-d2565b572b77baea/ │ ├── winapi-619d3257e8f28792/ │ └── winapi-x86_64-pc-windows-gnu-7e7040207fbb5417/ ├── deps/ │ └── <empty> ├── examples/ │ └── <empty> └── incremental/ └── <empty>
补充说明
1. 本地配置文件
.cargo/config.toml内容:
[target.aarch64-unknown-linux-gnu] linker = "aarch64-linux-gnu-gcc"
Cargo.toml内容:
[package] name = "brainfuck" version = "0.4.0" edition = "2021" [dependencies] console = "0.15.2" either = "1.8.0"
2. 测试发现
仅当指定编译目标时出现无二进制文件问题;使用默认系统目标编译时,可正常生成二进制文件。
问题原因
当使用--target参数进行交叉编译时,Cargo会将编译产物(包括二进制文件)放在--target-dir指定目录下的目标三元组子目录中,即路径为/tmp/${{ matrix.target }}/release,而非直接在/tmp/release。当前工作流的Debug步骤和产物上传步骤都错误地指向了/tmp/release,导致无法找到实际生成的二进制文件。
解决方法
修改工作流中的Debug步骤和产物上传步骤,将路径调整为正确的目标三元组子目录:
调整后的关键步骤
- name: Debug missing files run: | echo "target dir:" ls -a /tmp/${{ matrix.target }}/release echo "deps:" ls -a /tmp/${{ matrix.target }}/release/deps - name: Archive production artifacts uses: actions/upload-artifact@v3 with: name: ${{ matrix.target }} path: | /tmp/${{ matrix.target }}/release/*
此外,对于部分目标(如Windows系列),需确保cross能找到对应的交叉编译链接器,不过当前问题的核心是路径错误。
内容的提问来源于stack exchange,提问作者Spartan2909
相关产品推荐
相关产品推荐

