使用tensorflow的C ++ API构建错误

时间:2017-10-26 01:24:30

标签: c++ tensorflow

我正在使用cmake构建以下代码。我想使用tensorflow的C ++ API,但在编译代码时遇到了错误。

以下是错误:

  >/home/zifeng/software/clion-2017.2.3/bin/cmake/bin/cmake --build  
  >/home/zifeng/CLionProjects/tfcpp_demo/cmake-build-debug --target all -- -j 4  
  >[ 50%] Building CXX object CMakeFiles/tfcpp_demo.dir/src/main.cpp.o  
  >[100%] Linking CXX executable tfcpp_demo  
  >/usr/bin/ld: CMakeFiles/tfcpp_demo.dir/src/main.cpp.o: undefined reference to symbol '_ZNK10tensorflow6Status8ToStringB5cxx11Ev'  
  >//home/zifeng/CLionProjects/tfcpp_demo/./lib/libtensorflow_framework.so: error adding symbols: DSO missing from command line  
  >collect2: error: ld returned 1 exit status  
  >CMakeFiles/tfcpp_demo.dir/build.make:94: recipe for target 'tfcpp_demo' failed  
  >make[2]: *** [tfcpp_demo] Error 1  
  >CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/tfcpp_demo.dir/all' failed  
  >make[1]: *** [CMakeFiles/tfcpp_demo.dir/all] Error 2  
  >Makefile:83: recipe for target 'all' failed  
  >make: *** [all] Error 2 

这是我的代码:

#include <tensorflow/core/platform/env.h>
#include <tensorflow/core/public/session.h>

#include <iostream>

using namespace std;
using namespace tensorflow;

int main()
{
    Session* session;
    Status status = NewSession(SessionOptions(), &session);
    if (!status.ok()) {
        cout << status.ToString() << "\n";
        return 1;
    }
    cout << "Session successfully created.\n";
}

这是我的makefile:

cmake_minimum_required(VERSION 3.7)
project(tfcpp_demo)

set(CMAKE_CXX_STANDARD 11)
link_directories(./lib)

set(SOURCE_FILES
        src/main.cpp)

include_directories(
        /home/zifeng/software/tensorflow/tensorflow
        /home/zifeng/software/tensorflow/tensorflow/bazel-genfiles
        /home/zifeng/software/tensorflow/tensorflow/tensorflow/contrib/makefile/gen/protobuf/include
        /home/zifeng/software/tensorflow/tensorflow/tensorflow/contrib/makefile/downloads/nsync/public
        /usr/local/lib/python2.7/dist-packages/tensorflow/include
)
add_executable(tfcpp_demo  ${SOURCE_FILES})
target_link_libraries(tfcpp_demo tensorflow_cc)

2 个答案:

答案 0 :(得分:0)

我对您有同样的问题,并且已解决。

看起来有些libtensorflow .so文件在链接时丢失。

我通过使用两个命令来生成我的libtensorflow_cc.so:

bazel build --config=monolithic --config=cuda //tensorflow:libtensorflow_cc.so
bazel build --config=monolithic --config=cuda //tensorflow:libtensorflow_framework.so

这比一个命令要好。

bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so

答案 1 :(得分:-1)

#include <tensorflow/core/platform/env.h>
#include <tensorflow/core/public/session.h>

#include <iostream>

using namespace std;
using namespace tensorflow;

int main()
{
    Session* session;
    Status status = NewSession(SessionOptions(), &session);
    if (!status.ok()) {
        cout << status.ToString() << "\n";
        return 1;
    }
    cout << "Session successfully created.\n";
}