Bazel:测试看不到库 headers 但应用程序可以
Bazel: Tests cannot see library headers but app can
我有一个非常简单的 Bazel C++ 项目,我试图在其中实现 gtest,当包含测试文件时,它有 3 个 BUILD 文件。我遇到的问题是我库中的 headers 对应用程序目录可见,但对测试目录不可见,当我尝试构建我的简单测试时它失败了。
我的项目结构是:
- WORKSPACE
- app
|- BUILD
|- main.cpp
- lib
|- BUILD
|- foo
|- foo.hpp
|- foo.cpp
|- bar
|- bar.hpp
|- bar.cpp
- test
|- BUILD
|- lib_test.cpp
WORKSPACE
包含以下内容以启用 boost 和 gtest:
workspace(name = "my-app")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "com_github_nelhage_rules_boost",
commit = "fce83babe3f6287bccb45d2df013a309fa3194b8",
remote = "https://github.com/nelhage/rules_boost",
shallow_since = "1591047380 -0700",
)
load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps")
boost_deps()
git_repository(
name = "gtest",
remote = "https://github.com/google/googletest",
branch = "v1.10.x",
)
lib/BUILD
包含库的规则:
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(
name = "app-lib",
srcs = glob(["*/*.cpp"]),
hdrs = glob(["*/*.hpp"]),
deps = [
"@boost//:log",
"@boost//:date_time",
"@boost//:filesystem",
"@boost//:program_options",
],
visibility = [
"//app:__pkg__",
"//test:__pkg__",
],
)
app/BUILD
具有以下构建主二进制文件的方法:
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "my-app",
srcs = ["main.cpp"],
deps = [
"//lib:app-lib",
],
copts = ["-Ilib"],
)
test/BUILD
在我尝试让 gtest 工作时有以下内容
cc_test(
name = "lib-test",
srcs = [
"lib_test.cpp",
],
copts = ["-Ilib"],
deps = [
"//lib:app-lib",
"@gtest//:gtest",
"@gtest//:gtest_main",
],
)
现在,在我的 main.cpp
中,我已经将我的图书馆包括在内:
#include "foo/foo.hpp"
#include "bar/bar.hpp"
这可以毫无问题地构建我的二进制文件,但是在我的 lib_test.cpp
中,我尝试了相同的包含以及完整的相对路径包含:
#include "lib/foo/foo.hpp"
#include "lib/bar/bar.hpp"
然而,当我尝试 运行 bazel test test:lib-test
时,出现以下错误:
lib/foo/foo.hpp: No such file or directory
编辑 1:
我正在使用 VSCode 并且包含以下 c_cpp_properties.json
路径:
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/lib/",
"/home/user/Projects/my-app/bazel-my-app/external/boost"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
而不是 copts = ["-Ilib"]
,在 cc_library
上使用 includes = ["."]
。这告诉 Bazel,任何依赖于 cc_library
的东西都应该在包含路径上获取该目录,而不管到达那里的相对路径如何。
具体来说,删除所有 copts = ["-Ilib"]
,并将 lib/BUILD
更改为:
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(
name = "app-lib",
srcs = glob(["*/*.cpp"]),
hdrs = glob(["*/*.hpp"]),
includes = ["."],
deps = [
"@boost//:log",
"@boost//:date_time",
"@boost//:filesystem",
"@boost//:program_options",
],
visibility = [
"//app:__pkg__",
"//test:__pkg__",
],
)
我有一个非常简单的 Bazel C++ 项目,我试图在其中实现 gtest,当包含测试文件时,它有 3 个 BUILD 文件。我遇到的问题是我库中的 headers 对应用程序目录可见,但对测试目录不可见,当我尝试构建我的简单测试时它失败了。 我的项目结构是:
- WORKSPACE
- app
|- BUILD
|- main.cpp
- lib
|- BUILD
|- foo
|- foo.hpp
|- foo.cpp
|- bar
|- bar.hpp
|- bar.cpp
- test
|- BUILD
|- lib_test.cpp
WORKSPACE
包含以下内容以启用 boost 和 gtest:
workspace(name = "my-app")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "com_github_nelhage_rules_boost",
commit = "fce83babe3f6287bccb45d2df013a309fa3194b8",
remote = "https://github.com/nelhage/rules_boost",
shallow_since = "1591047380 -0700",
)
load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps")
boost_deps()
git_repository(
name = "gtest",
remote = "https://github.com/google/googletest",
branch = "v1.10.x",
)
lib/BUILD
包含库的规则:
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(
name = "app-lib",
srcs = glob(["*/*.cpp"]),
hdrs = glob(["*/*.hpp"]),
deps = [
"@boost//:log",
"@boost//:date_time",
"@boost//:filesystem",
"@boost//:program_options",
],
visibility = [
"//app:__pkg__",
"//test:__pkg__",
],
)
app/BUILD
具有以下构建主二进制文件的方法:
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "my-app",
srcs = ["main.cpp"],
deps = [
"//lib:app-lib",
],
copts = ["-Ilib"],
)
test/BUILD
在我尝试让 gtest 工作时有以下内容
cc_test(
name = "lib-test",
srcs = [
"lib_test.cpp",
],
copts = ["-Ilib"],
deps = [
"//lib:app-lib",
"@gtest//:gtest",
"@gtest//:gtest_main",
],
)
现在,在我的 main.cpp
中,我已经将我的图书馆包括在内:
#include "foo/foo.hpp"
#include "bar/bar.hpp"
这可以毫无问题地构建我的二进制文件,但是在我的 lib_test.cpp
中,我尝试了相同的包含以及完整的相对路径包含:
#include "lib/foo/foo.hpp"
#include "lib/bar/bar.hpp"
然而,当我尝试 运行 bazel test test:lib-test
时,出现以下错误:
lib/foo/foo.hpp: No such file or directory
编辑 1:
我正在使用 VSCode 并且包含以下 c_cpp_properties.json
路径:
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/lib/",
"/home/user/Projects/my-app/bazel-my-app/external/boost"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
而不是 copts = ["-Ilib"]
,在 cc_library
上使用 includes = ["."]
。这告诉 Bazel,任何依赖于 cc_library
的东西都应该在包含路径上获取该目录,而不管到达那里的相对路径如何。
具体来说,删除所有 copts = ["-Ilib"]
,并将 lib/BUILD
更改为:
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(
name = "app-lib",
srcs = glob(["*/*.cpp"]),
hdrs = glob(["*/*.hpp"]),
includes = ["."],
deps = [
"@boost//:log",
"@boost//:date_time",
"@boost//:filesystem",
"@boost//:program_options",
],
visibility = [
"//app:__pkg__",
"//test:__pkg__",
],
)