【问题标题】:Bazel and Gtest error: target ' ' not declared in packageBazel 和 Gtest 错误:目标“”未在包中声明
【发布时间】:2021-01-24 21:55:42
【问题描述】:

我对 Bazel 和 Gtest 非常陌生,并且一直在尝试在名为“species_test”.cpp 的 cpp 文件上运行测试。这是我不断收到的主要错误:

Wills-iMac:species Will$ bazel test species_test --test_output=all

ERROR: Skipping 'species_test': no such target '//species:species_test': target 'species_test' not declared in package 'species' defined by /Users/Will/git/orville-regularwills/species/BUILD.bazel
ERROR: no such target '//species:species_test': target 'species_test' not declared in package 'species' defined by /Users/Will/git/orville-regularwills/species/BUILD.bazel
INFO: Elapsed time: 0.473s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
FAILED: Build did NOT complete successfully (0 packages loaded)

我有两个 BUILD.bazel 文件: 这个是用于 .cpp 类实现的:

cc_library(
    name="species",
    srcs=glob(["*.cpp"]),
    hdrs=glob(["*.h"]),
    visibility=["//visibility:public"]
)

这是一个用于谷歌测试的文件,该文件具有species_test.cpp:

cc_test(
    name="species_test",
    srcs=["species_test.cpp"],
    copts=["-Iexternal/gtest/include"],
    deps=[
        "@gtest//::main",
        "//species"
    ]
)

这是我的工作区文件:

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
    name = "gtest",
    remote = "https://github.com/google/googletest",
    branch = "v1.10.x",
)

我不知道错误指的是什么,非常感谢任何指出我正确方向或清除任何东西的东西。

【问题讨论】:

  • 您好,您的存储库的结构是什么?我猜您的两个 BUILD 文件位于两个不同的目录中,因此对应于不同的包。那么,你尝试过bazel test //<test_directory>:species_test --test_output=all

标签: c++ visual-studio-code googletest bazel


【解决方案1】:

阅读https://docs.bazel.build/versions/master/build-ref.html。包(目录)应该只包含一个BUILD 文件。文件布局应该是这样的:

├── WORKSPACE
└── species
    ├── BUILD
    ├── species.cpp
    ├── species.hpp
    └── species_test.cpp

BUILD 文件:

cc_library(
    name="species",
    srcs=["species.cpp"],
    hdrs=["species.hpp"],
    visibility=["//visibility:public"]
)

cc_test(
    name="species_test",
    srcs=["species_test.cpp"],
    deps=[
        "@gtest//:main",
        ":species"
    ]
)

通知变更:

  • 我删除了glob 调用,因为在您的版本中species 库也会编译测试文件,这是不需要的
  • 我添加了species.hpp 标头,因为没有公共接口的库不能被其他目标使用
  • 您不需要copts 来设置包含路径,因为 Bazel 会为您执行此操作
  • @gtest//::main -> @gtest//:main:在 Bazel 中,您使用一个冒号来表示目标
  • 您的cc_test 规则取决于species 目标。 :species 是同一包中目标的标签。或者,您可以使用完整路径//species:species,其中第一个species 是包的名称(与根目录路径相同,即WORKSPACE 目录),:species 是您的cc_library 的名称目标

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 2015-02-20
    相关资源
    最近更新 更多