【问题标题】:tensorflow and bazel "undefined reference to" compilation errortensorflow和bazel“未定义引用”编译错误
【发布时间】:2015-11-24 18:10:24
【问题描述】:

我正在 tensorflow 框架中编写 C++ 代码,我想使用使用 makefile 编写的动态库。 在源代码中,我放置了头文件的路径:

#include "tensorflow/cc/include/libtrading/proto/tf_fix_client.h"

使用名为 fix_client(int argc, char **argv) 的函数 在 BUILD 文件中,我放置了动态库的路径,名为 libtrading.so:

cc_binary(
    name = "session",
    srcs = ["work/session.cc"],
    copts = tf_copts(),
    linkopts = [
        "-lpthread",
        "-lm",
        #for libtrading
        "-L/home/alessandro_mercadante/tensor_flows/tensorflow/tensorflow/cc/include/",
        "-ltrading",
    ],
    ...

bazel-build 检索到以下错误:

bazel-out/local_linux-opt/bin/tensorflow/cc/_objs/session/tensorflow/cc/work/session.o: In function `main':
session.cc:(.text.startup.main+0x2b): undefined reference to `fix_client(int, char**)'
collect2: error: ld returned 1 exit status

【问题讨论】:

  • 不是loader的问题,而是linker的问题,所以我觉得不是你猜的问题。
  • 是的,库的位置没有错误。这不是错误。
  • 我知道未定义参考问题背后的概念。我认为这与挡板的使用有关。希望有 bazel 编译方面的高手可以对这个问题提出一些有用的建议。

标签: c++ tensorflow bazel


【解决方案1】:

Bazel 要求声明所有依赖项,因此 TensorFlow 库应该在您的 deps 属性中。看起来您的目标并非如此(尤其是 tensorflow 包含的标志不合适)。

在快速查看 TensorFlow 构建文件后,我会说它需要以下 deps 属性:

 deps = [
     "//tensorflow/cc:cc_ops",
     "//tensorflow/core:kernels",
     "//tensorflow/core:tensorflow",
 ],

但我对 TensorFlow 本身真的很陌生。

你的 cc_binary 的 deps 属性是什么?

【讨论】:

  • 我把你列出的蘸酱放了。我发现问题只是将 C 库链接到 C++ 代码的问题。
【解决方案2】:

我发现了问题。 bazel中库的链接是正确的:问题是libtrading是一个C库,而tensorflow是在c++环境中构建的:所有链接的函数都需要包含在以下守卫中:

#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多