我有一个问题是Tensorflow c API .
我想在Mac上使用Tensorflow c api by g来构建cpp .

Install Tensorflow

通过阅读install_macinstall_c安装了Tensorflow .
然后,我可以像下面这样构建 .

(tensorflow) > $ ./a.out
hello from tensorflow c library version 1.4.0-rc1

我还可以使用bazel在api_guides上使用C API构建example.cc .

#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"

int main() {
  using namespace tensorflow;
  using namespace tensorflow::ops;
  Scope root = Scope::NewRootScope();
  // Matrix A = [3 2; -1 0]
  auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
  // Vector b = [3 5]
  auto b = Const(root, { {3.f, 5.f} });
  // v = Ab^T
  auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
  std::vector<Tensor> outputs;
  ClientSession session(root);
  // Run and fetch v
  TF_CHECK_OK(session.Run({v}, &outputs));
  // Expect outputs[0] == [19; -3]
  LOG(INFO) << outputs[0].matrix<float>();
  return 0;
}

但是,我想用g构建这个example.cc .

接下来,我编译了Tensorflow .

$ ./configure
...
$ bazel build -c opt //tensorflow:libtensorflow_cc.so
...
...
$ install -m 0644 bazel-bin/tensorflow/libtensorflow_cc.so /usr/local/lib/libtensorflow_cc.dylib
$ install -m 0644 bazel-bin/tensorflow/libtensorflow_framework.so /usr/local/lib/libtensorflow_framework.so
$ install_name_tool -id /usr/local/lib/libtensorflow_cc.dylib /usr/local/lib/libtensorflow_cc.dylib
$ install_name_tool -id /usr/local/lib/libtensorflow_framework.dylib /usr/local/lib/libtensorflow_framework.dylib

最后,我收集了包含文件到/ usr / local / include / tensorflow / include / .

run example.cpp by g++

我在上面的这个页面上构建了example.cc重命名的example.cpp .
然后,我遇到了下一个错误 .

(tensorflow) > $ /usr/bin/g++ example.cpp -I/usr/local/include/tensorflow/include -L/usr/local/lib -ltensorflow_cc -std=c++11
Undefined symbols for architecture x86_64:
  "tensorflow::TensorShapeBase<tensorflow::TensorShape>::TensorShapeBase(std::initializer_list<long long>)", referenced from:
      __ZN10tensorflow11TensorShapeCI2NS_15TensorShapeBaseIS0_EEESt16initializer_listIxE in example-4b6420.o
  "tensorflow::TensorShapeBase<tensorflow::TensorShape>::dims() const", referenced from:
      Eigen::DSizes<long, 2> tensorflow::TensorShape::AsEigenDSizesWithPadding<2>() const in example-4b6420.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is my Mac environment.

  • macOS High Sieera版本10.13.1

  • (tensorflow)> $ / usr / bin / g --version
    配置为: - prefix = / Applications / Xcode.app / Contents / Developer / usr --with-gxx-include-dir = / usr / include / c /4.2.1 Apple LLVM 9.0.0版(clang-900.0 . 38)目标:x86_64-apple-darwin17.2.0线程模型:posix InstalledDir:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

  • (tensorflow)> $ g --version
    g(Homebrew GCC 7.1.0)7.1.0版权所有(C)2017 Free Software Foundation,Inc . 这是免费软件;查看复制条件的来源 . 没有保修;甚至不适用于适销性或特定用途的适用性 .

  • python版本:3.5.2

  • tensorflow-1.4.0rc1

  • (tensorflow-1.4.0)> $ bazel version构建标签:0.7.0构建目标:bazel-out / darwin_x86_64-opt / bin / src / main / java / com / google / devtools / build / lib / bazel / BazelServer_deploy .jar构建时间:Wed Oct 18 14:25:46 2017(1508336746)Build timestamp:1508336746 build timestamp as int:1508336746

你有什么主意吗?
感谢您的回复 .