Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

c++ - Bazel and Gtest error: target ' ' not declared in package

I am extremely new to Bazel and Gtest and have been trying to run the tests on a cpp file called "species_test".cpp. This is the main error that I keep getting:

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)

I have two BUILD.bazel files: this one is for the .cpp class implementation:

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

and this one is for the file for google test that has species_test.cpp:

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

and here is my workspace file:

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",
)

I have no idea what the error is referring to and greatly appreciate anything that points me in the right direction or clears anything up.

question from:https://stackoverflow.com/questions/65876390/bazel-and-gtest-error-target-not-declared-in-package

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Read https://docs.bazel.build/versions/master/build-ref.html . Package (directory) should contain only one BUILD file. Files layout should looks like that one:

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

and BUILD file:

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"
    ]
)

Notice changes:

  • I have removed glob call, because in your version the species library will also compile the test file, which is not wanted
  • I have added species.hpp header, because library without a public interface is not usable by other targets
  • You do not need copts to set an include path as Bazel will do this for you
  • @gtest//::main -> @gtest//:main: in Bazel you use one colon to indicate a target
  • Your cc_test rule depend on species target. :species is label for a target in the same package. Alternatively you can use full path //species:species, where first species is the name of package (same as directory path from the root, which is a WORKSPACE directory) and the :species is a name of your cc_library target

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...