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

Bazel 7多仓库构建失败:无法找到lib2头文件

Bazel多仓库C++构建头文件找不到问题

我是Bazel新手,已成功搭建单仓库构建,但多仓库构建无法正常工作。目前有两个C++仓库,app2依赖lib2,我在WORKSPACE中配置了git_repository,希望构建app2时Bazel能自动拉取lib2,但执行bazel build //app2:app时,Bazel报错无法找到lib2的头文件。

错误信息

INFO: Analyzed target //app2:app2 (0 packages loaded, 0 targets configured).
ERROR: /home/bazel/.cache/bazel/_bazel_bazel/36a4e7d3b3c11620f7fa4375ad21cf55/external/lib2/BUILD.bazel:6:11: Compiling time_util.cpp failed: (Exit 1): gcc failed: error executing CppCompile command (from target @@lib2//:time_util)
/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++11' -MD -MF ... (remaining 21 arguments skipped)
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
external/lib2/time_util.cpp:1:10: fatal error: lib2/time_util.h: No such file or directory
1 | #include "lib2/time_util.h"
  |          ^~~~~~~~~~~~~~~~~~
compilation terminated.
ERROR: /home/bazel/testing/app2/BUILD.bazel:7:10: Compiling app2/app.cpp failed: (Exit 1): gcc failed: error executing CppCompile command (from target //app2:app2)
/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++11' -MD -MF ... (remaining 32 arguments skipped)
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
app2/app.cpp:1:10: fatal error: lib2/time_util.h: No such file or directory
1 | #include "lib2/time_util.h"
  |          ^~~~~~~~~~~~~~~~~~
compilation terminated.

相关Bazel配置文件

WORKSPACE.bazel

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "lib2",
    remote = "https://wszarmach@bitbucket.org/szarmach/lib2.git",
    branch = "main",
    # branch, tag, commit, hash
)

app2/BUILD.bazel

load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
    name = "app",
    srcs = ["app.cpp"],
    #deps = ["//lib2:time_util"],
    deps = ["@lib2//:time_util"],
    visibility = ["//visibility:public"],
)

lib2/BUILD.bazel

load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
    name = "time_util",
    srcs = ["time_util.cpp"],
    hdrs = ["time_util.h"],
    #hdrs = glob(["**/*.h"]),
    copts = ["-Ilib1"],
    includes = ["."],
    visibility = ["//visibility:public"],
)

解决方案

问题根源在于头文件路径配置和无效编译选项,调整步骤如下:

  1. 修正lib2的头文件结构与BUILD配置
    代码中使用#include "lib2/time_util.h",但当前lib2仓库的文件直接放在根目录,需要让头文件路径与引用匹配:

    • 在lib2仓库中创建lib2子目录,将time_util.h和time_util.cpp移入该目录
    • 修改lib2/BUILD.bazel:
      load("@rules_cc//cc:defs.bzl", "cc_library")
      
      cc_library(
          name = "time_util",
          srcs = ["lib2/time_util.cpp"],
          hdrs = ["lib2/time_util.h"],
          includes = ["."],
          visibility = ["//visibility:public"],
      )
      

    若不想移动文件,可将代码中的include语句改为#include "time_util.h",保持lib2/BUILD.bazel的includes = ["."]配置即可。

  2. 移除无效编译选项
    删除lib2/BUILD.bazel中的copts = ["-Ilib1"],该选项引用了不存在的路径,会干扰编译流程。

  3. 确认依赖引用正确性
    app2/BUILD.bazel中的deps = ["@lib2//:time_util"]配置正确,无需修改。

完成以上调整后,重新执行bazel build //app2:app即可正常构建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 19:20:23