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

如何在Bazel中调整头文件包含路径,移除lib目录前缀?

Yes, you can achieve this without changing your source code by adjusting the includes attribute in your lib cc_library targets. Here's how to do it:

Step 1: Update lib/BUILD.bazel

Add the includes = ["."] attribute to both lib1 and lib2 cc_library rules. This tells Bazel to add the lib/ directory to the include path for any target that depends on these libraries:

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

cc_library(
    name = "lib1",
    hdrs = glob(["lib1/**/*.hpp"]),
    srcs = glob(["lib1/**/*.cpp"]),
    copts = [
        ...,
    ],
    linkopts = [
        ...,
    ],
    visibility = [
        "//app:__pkg__",
        "//test:__pkg__",
    ],
    includes = ["."],  # Add this line
)

cc_library(
    name = "lib2",
    hdrs = glob(["lib2/**/*.hpp"]),
    srcs = glob(["lib2/**/*.cpp"]),
    copts = [
        ...,
    ],
    linkopts = [
        ...,
    ],
    visibility = [
        "//app:__pkg__",
        "//test:__pkg__",
    ],
    includes = ["."],  # Add this line
)

Step 2: Restore original include paths in main.cpp

Revert to your team's preferred syntax:

#include "lib1/lib1.hpp"
#include "lib2/lib2.hpp"

Why this works

The includes attribute propagates the lib/ directory as an include path to dependent targets (like your app binary). When compiling main.cpp, Bazel automatically adds -I<workspace-root>/lib to the compiler flags, letting the preprocessor resolve lib1/lib1.hpp to the actual file at lib/lib1/lib1.hpp.

Cleanup (if needed)

If you added manual include path flags (such as -I../ or -Ilib/) to your app target's copts, you can remove them now—this approach handles include paths in a modular, dependency-aware way.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 23:51:37