bazel C/C++ 项目中的短包含路径

Short include paths in bazel C/C++ project

我刚开始使用 bazel,我不知道如何让我的包含路径不必是完整路径。您可以在 src/main/main.c 文件中看到注释。

我的目录树:

.
├── WORKSPACE
├── src
│   ├── lib
│   │   ├── BUILD
│   │   └── mytypes.h
│   └── main
│       ├── BUILD
│       └── main.c
└── test
    └── BUILD

src/lib/mytypes.h 文件:

#ifndef MYTYPES_H
#define MYTYPES_H

typedef unsigned char byte_t;

#endif

src/lib/BUILD文件:

cc_library(
    name = "mytypes",
    #srcs = glob(["*.c"]),
    hdrs = glob(["*.h"]),
    visibility = ["//src/main:__pkg__"],
)

src/main/main.c 文件:

#include <stdio.h>
#include "src/lib/mytypes.h"  // I would like to use: #include "mytypes.h"

int main(int argc, char **argv)
{
    byte_t byte = 0xAF;
    printf("%X\n", byte);
    return 0;
}

src/main/BUILD 文件:

cc_library(
    name = "mytypes",
    #srcs = glob(["*.c"]),
    hdrs = glob(["*.h"]),
    visibility = ["//src/main:__pkg__"],
)

WORKSPACE 文件:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "com_google_googletest",
    sha256 = "7b100bb68db8df1060e178c495f3cbe941c9b058",
    strip_prefix = "googletest-release-1.11.0",
    urls = ["https://github.com/google/googletest/archive/release-1.11.0.tar.gz"],
)

cc_library.includes 就是您要找的。将您的 src/lib/BUILD 内容更改为:

cc_library(
    name = "mytypes",
    includes = ["."],
    hdrs = glob(["*.h"]),
    visibility = ["//src/main:__pkg__"],
)