在WSL中安装bazel后,不能执行bazel run
Author: ziqian-LCreated Jul 29, 2026Updated Jul 29, 2026
安装方式如下: 第 1 步:添加 Bazel 发行版 URI 作为软件包源
sudo apt install apt-transport-https curl gnupg -y
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor >bazel-archive-keyring.gpg
sudo mv bazel-archive-keyring.gpg /usr/share/keyrings
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/bazel-archive-keyring.gpg] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list第 2 步:安装和更新 Bazel
sudo apt update
sudo apt install bazel安装完成之后执行bazel run //practical_exercises/10_day_practice/day1:union报错:
WARNING: Target pattern parsing failed.
ERROR: Skipping '//practical_exercises/10_day_practice/day1:union': error loading package 'practical_exercises/10_day_practice/day1': Unable to find package for @@[unknown repo 'rules_cc' requested from @@]//cc:defs.bzl: The repository '@@[unknown repo 'rules_cc' requested from @@]' could not be resolved: No repository visible as '@rules_cc' from main repository.
ERROR: error loading package 'practical_exercises/10_day_practice/day1': Unable to find package for @@[unknown repo 'rules_cc' requested from @@]//cc:defs.bzl: The repository '@@[unknown repo 'rules_cc' requested from @@]' could not be resolved: No repository visible as '@rules_cc' from main repository.
INFO: Elapsed time: 0.056s
INFO: 0 processes.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target我的解决方式如下:
在CPlusPlusThings/MODULE.bazel下写入(如果没有该文件,可以执行bazel run //practical_exercises/10_day_practice/day1:union)
bazel_dep(name = "rules_cc", version = "0.0.9")再次运行,结果如下:
WARNING: For repository 'rules_cc', the root module requires module version [email protected], but got [email protected] in the resolved dependency graph. Please update the version in your MODULE.bazel or set --check_direct_dependencies=off
INFO: Analyzed target //practical_exercises/10_day_practice/day1:union (87 packages loaded, 468 targets configured).
INFO: Found 1 target...
Target //practical_exercises/10_day_practice/day1:union up-to-date:
bazel-bin/practical_exercises/10_day_practice/day1/union
INFO: Elapsed time: 1.010s, Critical Path: 0.01s
INFO: 1 process: 6 action cache hit, 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/practical_exercises/10_day_practice/day1/union
0 5 6 0Source: Light-City/CPlusPlusThings