简体   繁体   中英

Build c++ project of tensorflow with scons error

I installed libtensorflow_cc.so with bazel. But when I build a test project include example of tensorflow from code with scons, following error occured.

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

Example code from 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;
}

The 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"])

Other Infomation:

  • System: macOS High Sierra
  • gcc/g++: (Homebrew GCC 6.4.0) 6.4.0
  • Python: 2.7.10
  • CUDA: no
  • GPU: no
  • bazel: 0.9.0-homebrew

Creating a binary compiled against Tensorflow with bazel

  1. Clone the tensorflow repository.
  2. Inside tensorflow/tensorflow, create a working directory, let it be tftest .
  3. Add your C++ code that uses tensorflow, lets put that in test.cpp .
  4. Create a file called BUILD that looks like this:

    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" ])

  5. To build (with optimizations turned on):

    bazel build -c opt :project.

    The binary will be in bazel-bin/tensorflow/project .

The full manual is here tensorflow_cc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM