Yuji
Yuji

Reputation: 704

How can I compile correctly and execute the example of TensorFlow C++ API?

I tried the example of TensorFlow C++ API (https://www.tensorflow.org/api_guides/cc/guide) on macOS.

What I did is:

  1. Install bazel
  2. git clone --recursive https://github.com/tensorflow/tensorflow
  3. ./configure in the above cloned directory (disabled OpenCL support since my machine has poor GPU)
  4. make tensorflow/cc/example/example.cc and tensorflow/cc/example/BUILD according to the guide
  5. bazel run -c opt //tensorflow/cc/example:example

Compiling was succeeded but the following error occurred.

dyld: Symbol not found: __ZN10tensorflow10DEVICE_CPUE
  Referenced from: /private/var/tmp/_bazel_yuji/a8fcd93e6cfa4967cc9ea4c6e4cf2977/execroot/org_tensorflow/bazel-out/darwin_x86_64-py3-opt/bin/tensorflow/cc/example/example
  Expected in: flat namespace
 in /private/var/tmp/_bazel_yuji/a8fcd93e6cfa4967cc9ea4c6e4cf2977/execroot/org_tensorflow/bazel-out/darwin_x86_64-py3-opt/bin/tensorflow/cc/example/example

The example does not use the global variable DEVICE_CPU, so it seems tensorflow library issue.

How can I avoid it?

I tried the same things on Ubuntu and got many linker errors in the last stage of compiling such as,

error: undefined reference to <symbol name>

Upvotes: 1

Views: 599

Answers (1)

Yuji
Yuji

Reputation: 704

I finally found the solution.

BUILD below in the guide might not be suitable for the latest tensorflow.

cc_binary(
    name = "example",
    srcs = ["example.cc"],
    deps = [
        "//tensorflow/cc:cc_ops",
        "//tensorflow/cc:client_session",
        "//tensorflow/core:tensorflow",
    ],
)

I modified it according to BUILD in tensorflow/cc.

load(
    "//tensorflow:tensorflow.bzl",
    "tf_cc_binary",
)

tf_cc_binary(
    name = "example",
    srcs = ["example.cc"],
    deps = [
        "//tensorflow/cc:cc_ops",
        "//tensorflow/cc:client_session",
        "//tensorflow/core:tensorflow",
    ],
)

That's all.

Upvotes: 2

Related Questions