Bazel 找不到 C++ 示例代码的 tensorflow 包

Bazel doesn't find tensorflow packages for C++ example code

我尝试使 this example 工作,但每次我尝试使用 bazel 构建程序时,我都会收到此错误消息:

bazel build //code:label_image 
ERROR: /home/jonas/tensorflow/code/BUILD:12:1: no such package 'tensorflow': BUILD file not found on package path and referenced by '//code:label_image'.
ERROR: /home/jonas/tensorflow/code/BUILD:12:1: no such package 'tensorflow': BUILD file not found on package path and referenced by '//code:label_image'.
ERROR: /home/jonas/tensorflow/code/BUILD:12:1: no such package 'tensorflow': BUILD file not found on package path and referenced by '//code:label_image'.
ERROR: /home/jonas/tensorflow/code/BUILD:12:1: no such package 'tensorflow': BUILD file not found on package path and referenced by '//code:label_image'.
ERROR: /home/jonas/tensorflow/code/BUILD:12:1: no such package 'tensorflow': BUILD file not found on package path and referenced by '//code:label_image'.
ERROR: Analysis of target '//code:label_image' failed; build aborted.
INFO: Elapsed time: 1.261s

我将 github 中的确切源代码保存在名为 code 的目录中。我通过 pip 在(活动的)虚拟环境中安装了 tensorflowpip3 install --upgrade tensorflow。我用拱门 linux.

为什么 bazel 找不到合适的包?我对 bazel/tensorflow 很陌生。这些包保存在哪里?我必须在某处明确指定它们吗?

通常,从使用 Bazel 的项目中提取子文件夹并单独构建它是行不通的。

如果查看 label_image 文件夹的 BUILD 文件,您将看到 C++ 二进制文件的定义:

cc_binary(
    name = "label_image",
    srcs = [
        "main.cc",
    ],
    linkopts = select({
        "//tensorflow:android": [
            "-pie",
            "-landroid",
            "-ljnigraphics",
            "-llog",
            "-lm",
            "-z defs",
            "-s",
            "-Wl,--exclude-libs,ALL",
        ],
        "//conditions:default": ["-lm"],
    }),
    deps = select({
        "//tensorflow:android": [
            # cc:cc_ops is used to include image ops (for label_image)
            # Jpg, gif, and png related code won't be included
            "//tensorflow/cc:cc_ops",
            "//tensorflow/core:android_tensorflow_lib",
            # cc:android_tensorflow_image_op is for including jpeg/gif/png
            # decoder to enable real-image evaluation on Android
            "//tensorflow/core/kernels:android_tensorflow_image_op",
        ],
        "//conditions:default": [
            "//tensorflow/cc:cc_ops",
            "//tensorflow/core:core_cpu",
            "//tensorflow/core:framework",
            "//tensorflow/core:framework_internal",
            "//tensorflow/core:lib",
            "//tensorflow/core:protos_all_cc",
            "//tensorflow/core:tensorflow",
        ],
    }),
)

此规则告诉 Bazel label_image 二进制文件需要构建什么。值得注意的是,它具有依赖项 (deps) 和 link 选项 (linkopts),它们指向 tensorflow 工作空间的根目录 (//tensorflow,由 WORKSPACE 定义文件),它从您提取的子文件夹中丢失。这就是 Bazel 抱怨找不到包 tensorflow.

的原因

构建 label_image 二进制文件的最简单方法是在 tensorflow 项目中构建它,因为路径已经设置好了。