2

I am writing a C++ code in tensorflow framework and I want to use a dynamic library written using makefile. In source code I put the path to header file:

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

to use a function called fix_client(int argc, char **argv) and in the BUILD file I put the path to the dynamic library, called 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 retrieves me an error of:

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
  • It is not a problem of loader, but of linker, so I think that it is not the problem that you guess. – alessandro mercadante Nov 24 '15 at 19:55
  • 1
    Possible duplicate: [What is an undefined reference and how to resolve it](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). – Thomas Matthews Nov 24 '15 at 21:22
  • Yes, there is no error in location of the library. It's not an error. – alessandro mercadante Nov 25 '15 at 08:51
  • I know the concepts behind undefined reference problems. I think that is something related with the use of bezel. I hope that there is somebody expert in bazel compilation that can give some useful suggestions to the question. – alessandro mercadante Nov 25 '15 at 08:52

2 Answers2

2

Bazel requires all dependencies to be declared, so the TensorFlow library should be in your deps attribute. It looks like it is not the case in your target (especially the flag for tensorflow includes is out of place).

After a quick look at TensorFlow build file I would say it needs the following deps attribute:

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

But I am really unfamiliar with TensorFlow itself.

What's the deps attribute of your cc_binary?

2

I found the problem. The linking of library in bazel is correct: the problem was that libtrading is a C library and tensorflow is built in a c++ environment: all the functions linked need to be included in the following guards:

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