用scons错误构建tensorflow的c ++项目

时间:2018-04-02 02:02:59

标签: c++ tensorflow

我用bazel安装了libtensorflow_cc.so。但是,当我构建一个测试项目包含来自code的带有scons的tensorflow示例时,发生以下错误。

g++ -o bin/tftest test/test.o -L/usr/local/lib -Llib -ltensorflow_cc
Undefined symbols for architecture x86_64:
  "tensorflow::TfCheckOpHelperOutOfLine[abi:cxx11](tensorflow::Status const&, char const*)", referenced from:
      _main in test.o
  "tensorflow::internal::CheckOpMessageBuilder::NewString[abi:cxx11]()", referenced from:
      std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >* tensorflow::internal::MakeCheckOpString<int, int>(int const&, int const&, char const*) in test.o
      std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >* tensorflow::internal::MakeCheckOpString<unsigned long, unsigned long>(unsigned long const&, unsigned long const&, char const*) in test.o
      std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >* tensorflow::internal::MakeCheckOpString<long long, long long>(long long const&, long long const&, char const*) in test.o
  "tensorflow::ClientSession::Run(std::vector<tensorflow::Output, std::allocator<tensorflow::Output> > const&, std::vector<tensorflow::Tensor, std::allocator<tensorflow::Tensor> >*) const", referenced from:
      _main in test.o
  "tensorflow::Scope::WithOpName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const", referenced from:
      _main in test.o
ld: symbol(s) not found for architecture x86_64

来自tensorflow的示例代码:

// tensorflow/cc/example/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;
}

SConscript:

import os, sys, string

env = Environment(CC='g++', CCFLAGS="-std=c++11 -Wall -Wextra -O3 -march=native -fPIC -fdiagnostics-color=always")

cpppath=["./", "./src", "/usr/local/include/tf", "/usr/local/include/eigen3/"]
env.Append(CPPPATH = cpppath)

libpath=["/usr/local/lib"]
env.Append(LIBPATH = libpath)

libs = ["tensorflow_cc"]
env.Append(LIBS = libs)

Export("env")

env.Program(target = "bin/tftest", source=["test/test.cpp"])

其他信息:

  • 系统:macOS High Sierra
  • gcc / g ++ :( Homebrew GCC 6.4.0)6.4.0
  • Python:2.7.10
  • CUDA:没有
  • GPU:没有
  • bazel:0.9.0-homebrew

1 个答案:

答案 0 :(得分:0)

使用bazel

创建针对Tensorflow编译的二进制文件
  1. 克隆tensorflow存储库。
  2. 在tensorflow / tensorflow内部,创建一个工作目录,让它为test.cpp
  3. 添加使用tensorflow的C ++代码,让我们将其放在BUILD
  4. 创建一个名为cc_binary( name = "project", srcs = ["test.cpp"], linkopts = ["-lopencv_core","-lopencv_imgproc", "-lopencv_highgui", "-Wl,--version-script=tensorflow/tf_version_script.lds"], copts = ["-I/usr/local/include/", "-O3"], deps = [ "//tensorflow/core:tensorflow" ])的文件,如下所示:

    bazel build -c opt :project.

  5. 要构建(启用优化):

    bazel-bin/tensorflow/project

    二进制文件将位于create()

  6. 完整的手册在tensorflow_cc