在这里,您将找到一些最常见的使用场景,了解如何使用 Bazel 构建 C++ 项目。如果您尚未开始使用 Bazel 构建 C++ 项目,请先完成教程 Bazel 简介:构建 C++ 项目。
如需了解 cc_library 和 hdrs 头文件,请参阅 cc_library。
在目标中添加多个文件
您可以使用 glob 在单个目标中添加多个文件。 例如:
cc_library(
name = "build-all-the-files",
srcs = glob(["*.cc"]),
hdrs = glob(["*.h"]),
)
借助此目标,Bazel 将构建在与包含此目标的 BUILD 文件相同的目录中找到的所有 .cc 和 .h 文件(不包括子目录)。
使用传递性 include
如果某个文件包含头文件,则以该文件作为来源的任何规则(即
在 srcs、hdrs 或 textual_hdrs 属性中包含该文件)都应
依赖于所包含头文件的库规则。反之,只需将直接依赖项指定为依赖项即可。例如,假设 sandwich.h 包含 bread.h,而 bread.h 包含 flour.h。sandwich.h
不包含 flour.h(谁想在三明治里放面粉?),因此 BUILD 文件如下所示:
cc_library(
name = "sandwich",
srcs = ["sandwich.cc"],
hdrs = ["sandwich.h"],
deps = [":bread"],
)
cc_library(
name = "bread",
srcs = ["bread.cc"],
hdrs = ["bread.h"],
deps = [":flour"],
)
cc_library(
name = "flour",
srcs = ["flour.cc"],
hdrs = ["flour.h"],
)
在这里,sandwich 库依赖于 bread 库,而 bread 库又依赖于 flour 库。
添加 include 路径
有时,您无法(或不想)将 include 路径根植于工作区根目录。现有库可能已有与工作区中的路径不匹配的 include 目录。例如,假设您有以下目录结构:
└── my-project
├── legacy
│ └── some_lib
│ ├── BUILD
│ ├── include
│ │ └── some_lib.h
│ └── some_lib.cc
└── WORKSPACE
Bazel 会将 some_lib.h 预期为以
legacy/some_lib/include/some_lib.h 的形式添加,但假设 some_lib.cc 添加了
"some_lib.h"。如需使该 include 路径有效,legacy/some_lib/BUILD 需要指定 some_lib/include 目录是一个 include 目录:
cc_library(
name = "some_lib",
srcs = ["some_lib.cc"],
hdrs = ["include/some_lib.h"],
copts = ["-Ilegacy/some_lib/include"],
)
这对于外部依赖项尤其有用,因为它们的头文件必须以 / 前缀添加。
添加外部库
假设您使用的是 Google 测试。
您可以使用 WORKSPACE 文件中的某个代码库函数下载 Google 测试,并使其在您的代码库中可用:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "gtest",
url = "https://github.com/google/googletest/archive/release-1.10.0.zip",
sha256 = "94c634d499558a76fa649edb13721dce6e98fb1e7018dfaeba3cd7a083945e91",
build_file = "@//:gtest.BUILD",
)
然后,创建 gtest.BUILD,这是一个用于编译 Google 测试的 BUILD 文件。
Google 测试有几个“特殊”要求,这使得其 cc_library 规则更加复杂:
googletest-release-1.10.0/src/gtest-all.cc#includesgoogletest-release-1.10.0/src/中的所有其他文件:将其从编译中排除,以防止出现重复符号的链接错误。它使用相对于
googletest-release-1.10.0/include/目录的头文件 ("gtest/gtest.h"),因此您必须 将该目录添加到 include 路径。它需要链接到
pthread,因此请将其添加为linkopt。
因此,最终规则如下所示:
cc_library(
name = "main",
srcs = glob(
["googletest-release-1.10.0/src/*.cc"],
exclude = ["googletest-release-1.10.0/src/gtest-all.cc"]
),
hdrs = glob([
"googletest-release-1.10.0/include/**/*.h",
"googletest-release-1.10.0/src/*.h"
]),
copts = [
"-Iexternal/gtest/googletest-release-1.10.0/include",
"-Iexternal/gtest/googletest-release-1.10.0"
],
linkopts = ["-pthread"],
visibility = ["//visibility:public"],
)
这有点混乱:所有内容都以 googletest-release-1.10.0 为前缀,这是归档文件结构的副产品。您可以通过添加 strip_prefix 属性,使 http_archive 剥离此前缀:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "gtest",
url = "https://github.com/google/googletest/archive/release-1.10.0.zip",
sha256 = "94c634d499558a76fa649edb13721dce6e98fb1e7018dfaeba3cd7a083945e91",
build_file = "@//:gtest.BUILD",
strip_prefix = "googletest-release-1.10.0",
)
然后,gtest.BUILD 将如下所示:
cc_library(
name = "main",
srcs = glob(
["src/*.cc"],
exclude = ["src/gtest-all.cc"]
),
hdrs = glob([
"include/**/*.h",
"src/*.h"
]),
copts = ["-Iexternal/gtest/include"],
linkopts = ["-pthread"],
visibility = ["//visibility:public"],
)
现在,cc_ 规则可以依赖于 @gtest//:main。
编写和运行 C++ 测试
例如,您可以创建一个测试 ./test/hello-test.cc,如下所示:
#include "gtest/gtest.h"
#include "main/hello-greet.h"
TEST(HelloTest, GetGreet) {
EXPECT_EQ(get_greet("Bazel"), "Hello Bazel");
}
然后,为您的测试创建 ./test/BUILD 文件:
cc_test(
name = "hello-test",
srcs = ["hello-test.cc"],
copts = ["-Iexternal/gtest/include"],
deps = [
"@gtest//:main",
"//main:hello-greet",
],
)
如需使 hello-greet 对 hello-test 可见,您必须将
"//test:__pkg__", 添加到 visibility 属性中 ./main/BUILD。
现在,您可以使用 bazel test 运行测试。
bazel test test:hello-test
这将生成以下输出:
INFO: Found 1 test target...
Target //test:hello-test up-to-date:
bazel-bin/test/hello-test
INFO: Elapsed time: 4.497s, Critical Path: 2.53s
//test:hello-test PASSED in 0.3s
Executed 1 out of 1 tests: 1 test passes.
添加对预编译库的依赖项
如果您想使用仅具有已编译版本的库(例如,头文件和 .so 文件),请将其封装在 cc_library 规则中:
cc_library(
name = "mylib",
srcs = ["mylib.so"],
hdrs = ["mylib.h"],
)
这样,工作区中的其他 C++ 目标就可以依赖于此规则。