我已经在Mac上编译了Tensorflow 2.0,编译成功后,我将所有文件复制到了include文件夹,这是我的include文件夹树:
include
├── Eigen
│ └── src
├── absl
│ ├── algorithm
│ ├── base
│ ├── container
│ ├── copts
│ ├── debugging
│ ├── flags
│ ├── hash
│ ├── memory
│ ├── meta
│ ├── numeric
│ ├── strings
│ ├── synchronization
│ ├── time
│ ├── types
│ └── utility
├── google
│ └── protobuf
├── tensorflow
│ ├── c
│ ├── cc
│ ├── compiler
│ ├── core
│ └── stream_executor
├── third_party
│ ├── FP16
│ ├── android
│ ├── aws
│ ├── boringssl
│ ├── clang_toolchain
│ ├── eigen3
│ ├── fft2d
│ ├── flatbuffers
│ ├── git
│ ├── gpus
│ ├── grpc
│ ├── hadoop
│ ├── highwayhash
│ ├── hwloc
│ ├── icu
│ ├── jpeg
│ ├── keras_applications_archive
│ ├── kissfft
│ ├── llvm
│ ├── mkl
│ ├── mkl_dnn
│ ├── mlir
│ ├── mpi
│ ├── nasm
│ ├── nccl
│ ├── ngraph
│ ├── opencl_headers
│ ├── ortools
│ ├── pasta
│ ├── protobuf
│ ├── py
│ ├── python_runtime
│ ├── sycl
│ ├── systemlibs
│ ├── tensorrt
│ └── toolchains
└── unsupported
├── Eigen
├── bench
├── doc
└── test
当我运行构建命令时:
g++ -g main.cpp -std=c++17 -I /tf/include -L /tf/include/tensorflow -o main --debug -v -ltensorflow_framework -ltensorflow_cc -l protobuf -Wl,-rpath /tf/include/tensorflow
使用main.cpp
和Session
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/cc/framework/scope.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/cc/client/client_session.h"
using namespace std;
using namespace tensorflow;
using namespace tensorflow::ops;
int main()
{
Session* session;
Status status = NewSession(SessionOptions(), &session);
if (!status.ok()) {
cout << status.ToString() << "\n";
}
cout << "Session created.\n";
return 0;
}
我收到成功的会话消息:
2019-11-16 14:44:32.739109: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fd4dc5a5790 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2019-11-16 14:44:32.739129: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
Session created.
但是如果我使用ClientSession
:
int main()
{
const Scope root = Scope::NewRootScope();
auto A = tensorflow::ops::Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
auto b = tensorflow::ops::Const(root, { {3.f, 5.f} });
auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
tensorflow::ClientSession session(root);
std::vector<tensorflow::Tensor> outputs;
auto t = session.Run({v}, &outputs);
return 0;
}
并尝试使用上述g ++ build命令进行编译,它会引发错误:
Undefined symbols for architecture x86_64:
"tensorflow::ClientSession::ClientSession(tensorflow::Scope const&)", referenced from:
second() in main-7d2388.o
"tensorflow::ClientSession::~ClientSession()", referenced from:
second() in main-7d2388.o
"tensorflow::ClientSession::Run(std::__1::vector<tensorflow::Output, std::__1::allocator<tensorflow::Output> > const&, std::__1::vector<tensorflow::Tensor, std::__1::allocator<tensorflow::Tensor> >*) const", referenced from:
second() in main-7d2388.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
忽略我在g ++ build命令中包含-undefined dynamic_lookup
的错误,构建成功完成,现在当我运行/.main
时遇到此错误:
dyld: lazy symbol binding failed: Symbol not found: __ZN10tensorflow13ClientSessionC1ERKNS_5ScopeE
Referenced from: /private/var/www/cpp/png2stl/./main
Expected in: flat namespace
dyld: Symbol not found: __ZN10tensorflow13ClientSessionC1ERKNS_5ScopeE
Referenced from: /private/var/www/cpp/png2stl/./main
Expected in: flat namespace
[1] 37062 abort ./main
我还尝试使用选项--config=monolithic
编译TensorFlow,但它显示相同的错误。
这是otool -L libtensorflow_cc.so
include/tensorflow/libtensorflow_cc.so:
@rpath/libtensorflow_cc.so.2 (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.7.0)
@rpath/libtensorflow_framework.2.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.0.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1673.126.0)
/System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 59306.41.2)
/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1673.126.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
和otool -L libtensorflow_framework.so
include/tensorflow/libtensorflow_framework.so:
@rpath/libtensorflow_framework.so.2 (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.7.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1673.126.0)
/System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 59306.41.2)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.0.0)
/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1673.126.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)