3

伙计们,

我正在尝试将 .h 和静态库链接到我的 tensorflow 程序中。我的标题在

/usr/local/include/lcm

和静态/共享库(.so等)在

/usr/local/lib

但巴泽尔抱怨说他们不存在,或者找不到他们。这是我的 BUILD 文件中的代码:

package(default_visibility = ["//tensorflow:internal"])
licenses(["notice"])  # Apache 2.0
exports_files(["LICENSE"])

# LCM shared libraries path
cc_library(
    name = "lcm_lib",
    srcs = glob([
        "*.so",
    ]),
    copts = ["-L/usr/local/lib"],
    linkopts = ["-pthread", "-shared"],
    visibility = ["//visibility:public"],
)

# LCM header libraries path
cc_library(
    name = "lcm_headers",
    hdrs = glob([
        "include/**/*.h",
    ]),
    copts = ["-L/usr/local/include"],
    linkopts = ["-pthread"],
    visibility = ["//visibility:public"],
)

cc_binary(
    name = "myProject",
    srcs = [
        "main.cc",
    ],
    linkopts = ["-lm"],
    deps = [
        "//tensorflow/cc:cc_ops",
        "//tensorflow/core:framework_internal",
        "//tensorflow/core:tensorflow",
    ],
)

filegroup(
    name = "all_files",
    srcs = glob(
        ["**/*"],
        exclude = [
            "**/METADATA",
            "**/OWNERS",
            "bin/**",
            "gen/**",
        ],
    ),
    visibility = ["//tensorflow:__subpackages__"],
)

如果我删除 LCM 相关代码(来自 BUILD 和 main.cc),那么我的程序将构建并运行。当我包含 LCM 时,我会收到错误消息,指出 lcm::LCM::~LCM() 未定义,并且找不到 liblcm.so。现在,我的非 tensorflow 代码(或我的大部分项目)正在运行 cmake,并且可以找到 LCM 和我需要的其余库(openCV 等)。我在 CMakeList.txt 中使用命令,例如:

# search path for LCM header files
set(LCM_IncludeSearchPaths
  /usr/include/
  /usr/local/include/
  /opt/local/include
)
# search path for LCM static/dynamic libraries
set(LCM_LibrarySearchPaths
  /usr/lib/
  /usr/local/lib/
  /opt/local/lib/
)
find_path(LCM_INCLUDE_DIR
    NAMES lcm/lcm.h
    HINTS ${LCM_IncludeSearchPaths}
)   
FIND_LIBRARY(LCM_LIBS
  NAMES lcm
  HINTS ${LCM_LibrarySearchPaths}
  PATH_SUFFIXES lib
)

这一切都有效。但它不适用于 tensorflow 和 Bazel

这是我的构建和工作空间文件,位于同一目录中:

这是我感动的 WORKSPACE 文件:

# LCM static libraries
new_local_repository(
    name = "lcm_libs",
    # pkg-config --variable=libdir x11
    path = "/usr/local/lib",
    build_file_content = """
cc_library(
    name = "liblcm",
    srcs = ["liblcm.so"],
    visibility = ["//visibility:public"],
)
""",
)

# LCM header files
new_local_repository(
    name = "lcm_headers",
    # pkg-config --variable=libdir x11
    path = "/usr/local/include",
    build_file_content = """
cc_library(
    name = "lcm",
    hdrs = glob([
        "lcm/*.h", "lcm/*.hpp",
    ]),
    visibility = ["//visibility:public"],
)
""",
)

# bind to a name to avoid using the "actual" format
#bind(
#    name = "liblcm",
#    actual = "@lcm_libs//:liblcm",
#)
#bind(
#    name = "lcm",
#    actual = "@lcm_headers//:lcm",
#)
#

我的构建:

# Description:
#   Tensorflow C++ inference example for labeling images.

package(default_visibility = ["//tensorflow:internal"])
licenses(["notice"])  # Apache 2.0
exports_files(["LICENSE"])

cc_binary(
    name = "facialFatigue",
    srcs = [
        "main.cc",
    ],
    linkopts = ["-lm"],
    deps = [
        "//tensorflow/cc:cc_ops",
        "//tensorflow/core:framework_internal",
        "//tensorflow/core:tensorflow",
        "@lcm_libs//:liblcm",
        "@lcm_headers//:lcm",
    ],
)

filegroup(
    name = "all_files",
    srcs = glob(
        ["**/*"],
        exclude = [
            "**/METADATA",
            "**/OWNERS",
            "bin/**",
            "gen/**",
        ],
    ),
    visibility = ["//tensorflow:__subpackages__"],
)

对不起,很长的问题:-(

4

1 回答 1

1

Bazel 在沙箱中执行操作(在您的情况下为 C++ 编译操作),以确保密封性。当动作的输入改变时,这需要是正确的。因此,您必须告诉 Bazel所有输入,包括系统输入。

但是当然你可以依赖系统库,看看local_repository rule。您可能还会在 bazel-discuss@线程中发现此示例很有帮助。

于 2017-04-05T08:24:31.923 回答