alessandro mercadante
alessandro mercadante

Reputation: 375

tensorflow and bazel "undefined reference to" compilation error

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

Upvotes: 2

Views: 6553

Answers (2)

alessandro mercadante
alessandro mercadante

Reputation: 375

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

Upvotes: 2

Damien Martin-Guillerez
Damien Martin-Guillerez

Reputation: 2370

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?

Upvotes: 2

Related Questions