【问题标题】:Glob not found in bazel在 bazel 中找不到 Glob
【发布时间】:2018-02-16 06:43:42
【问题描述】:

我目前正在使用 Bazel 构建一个 C++ 项目。这是我的 WORKSPACE 文件:

cc_library(
name = "Boost",
srcs = glob(["/usr/local/lib/libboost*.so"]),
hdrs = glob(["/home/duttama/boost_1_55_0/**/*.h"]),
)


cc_library(
name = "OpenCV",
srcs = glob(["/usr/lib/x86_64-linux-gnu/libopencv*.so"]),
hdrs = glob(["/home/duttama/opencv-2.4.13/**/*.h"]),
)


cc_library(
name = "AffectivaSources",
srcs = glob(["/root/affdex-sdk/lib/*.so"]),
hdrs = glob(["/root/affdex-sdk/include/*.h"]),
)

这是我的 BUILD 文件:

cc_binary(
name = "video-demo",
srcs = ["video-demo.cpp"],
deps = ["//lib:OpenCV",
            "//lib:AffectivaSources",
            "//lib:Boost",
],
)    

当我运行它时,我得到了这个错误:

ERROR: /root/sdk-samples/WORKSPACE:17:12: Traceback (most recent call last):
File "/root/sdk-samples/WORKSPACE", line 15
    cc_library(name = "AffectivaSources", srcs = ..."]), ..."]))
File "/root/sdk-samples/WORKSPACE", line 17, in cc_library
    glob
name 'glob' is not defined

我的问题是为什么找不到 glob 函数。我不确定在 WORKSPACE 文件中还要指定什么或如何导入这个 glob 函数。

【问题讨论】:

    标签: c++ opencv build bazel


    【解决方案1】:

    WORKSPACE 文件不适用于cc_binary/cc_rules;它用于定义项目范围的规则,例如外部依赖项,并且 glob 函数在 WORKSPACE 文件中不可用。有关 WORKSPACE 规则,请参阅 https://docs.bazel.build/versions/master/be/workspace.html

    您似乎正在将 c++ 依赖项导入您的项目:查看new_local_repository 规则here。这允许您指向本地系统中的文件夹。它将要求您为每个外部依赖项编写包含相应 cc_library 规则的 BUILD 文件。

    例如,在您的 WORKSPACE 文件中:

    new_local_repository(
      name = "affectivalibrary",
      path = "/root/affdex-sdk/",
      build_file = "BUILD.affectiva",
    )
    

    在你的项目中,创建一个BUILD.affectiva:

    cc_library(
      name = "aff",
      srcs = glob(["lib/*.so"]),
      hdrs = glob(["include/*.h"]),
    )
    

    完成此操作后,您可以使用以下语法在 BUILD 文件中依赖它们:

    cc_binary(
      name = "video-demo",
      srcs = ["video-demo.cpp"],
      deps = ["@affectivalibrary//:aff", ...]
    )
    

    我还看到您在 srcs/hdrs 属性中使用绝对文件路径 - glob 模式不能是绝对的。请参考这里的BUILD文件目标标签语法:https://docs.bazel.build/versions/master/build-ref.html#lexi

    如果您仍然感到困惑,Bazel 存储库中有一个示例 c++ 项目:https://github.com/bazelbuild/bazel/tree/master/examples/cpp

    【讨论】:

      猜你喜欢
      • 2012-01-25
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多