You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Bazel输出符号链接的存放位置?

如何让Bazel将输出符号链接放到指定目录

问题描述

我使用Bazel作为C++构建系统,每次构建项目后,根目录都会被bazel-bin、bazel-out、bazel-pipeline和bazel-testlogs这些符号链接弄得杂乱不堪。想让Bazel把这些符号链接放置到./out目录下,试过--output_base=./out和--output_user_root=./out参数,但符号链接仍出现在根目录中。

期望执行bazel [options] build后的项目结构:

ProjectRoot
├── bin
│   ├── App.cpp
│   └── BUILD.bazel
├── lib
│   ├── Lib1
│   │   ├── Lib1.cpp
│   │   ├── Lib1.hpp
│   │   └── BUILD.bazel
│   ├── Lib2
│   │   ├── Lib2.cpp
│   │   ├── Lib2.hpp
│   │   └── BUILD.bazel
│   └── BUILD.bazel
├── out
│   ├── bazel-bin
│   │   ├── stuff
│   │   └── ...
│   ├── bazel-out
│   │   ├── stuff
│   │   └── ...
│   ├── bazel-pipeline
│   │   ├── stuff
│   │   └── ...
│   └── bazel-testlogs
│       ├── stuff
│       └── ...
├── test
│   ├── test.cpp
│   └── BUILD.bazel
└── WORKSPACE.bazel

当前项目结构:

ProjectRoot
├── bazel-bin
│   ├── stuff
│   └── ...
├── bazel-out
│   ├── stuff
│   └── ...
├── bazel-pipeline
│   ├── stuff
│   └── ...
├── bazel-testlogs
│   ├── stuff
│   └── ...
├── bin
│   ├── App.cpp
│   └── BUILD.bazel
├── lib
│   ├── Lib1
│   │   ├── Lib1.cpp
│   │   ├── Lib1.hpp
│   │   └── BUILD.bazel
│   ├── Lib2
│   │   ├── Lib2.cpp
│   │   ├── Lib2.hpp
│   │   └── BUILD.bazel
│   └── BUILD.bazel
├── out
├── test
│   ├── test.cpp
│   └── BUILD.bazel
└── WORKSPACE.bazel

解决方案

这是直接控制符号链接生成路径的参数,执行构建命令时添加该参数:

bazel build --symlink_prefix=./out/ //...

该参数会让Bazel将所有输出符号链接(bazel-bin、bazel-out等)都创建到./out目录下,完全匹配你期望的项目结构。

方法2:配置.bazelrc文件简化操作

在项目根目录创建或修改.bazelrc文件,添加以下内容:

build --symlink_prefix=./out/

之后每次执行bazel build时,会自动应用该参数,无需手动重复输入。

关键说明

  • --output_base和--output_user_root控制的是Bazel内部构建缓存与实际输出文件的存储位置,并非符号链接的生成位置,因此之前使用这两个参数无法达到预期效果是正常的。
  • 若根目录已存在旧的符号链接,建议手动删除,避免目录结构混乱。

内容的提问来源于stack exchange,提问作者OpticalMagician

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 07:15:25