简体   繁体   English

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

[英]Build errors with tensorflow's C++ API

I'm building the following code with cmake . 我正在使用cmake构建以下代码。 I want to use tensorflow's C++ API,but I encountered errors when compiling the code. 我想使用tensorflow的C ++ API,但是在编译代码时遇到错误。

Here are the errors: 错误如下:

  >/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 

Here is my code: 这是我的代码:

#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";
}

Here is my makefile: 这是我的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)

I have same problem with you and I fix it. 我对您有同样的问题,并且已解决。

It looks some libtensorflow .so files are missing at linking. 看起来有些libtensorflow .so文件在链接时丢失。

I generate my libtensorflow_cc.so by using two command: 我通过使用两个命令来生成我的libtensorflow_cc.so:

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

It is better than one command. 它比一个命令要好。

bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so
#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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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