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

标签 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";
}

这是我的生成文件:

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)

最佳答案

我和你有同样的问题,我解决了。

看起来有些 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

关于c++ - 使用 tensorflow 的 C++ API 构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46944416/

相关文章:

c++ - 确定 QVariant 是 String 还是 Int

c++ - 练习: split the array values into positive and negative arrays

python - 如何在keras中构建嵌入层

c++ - 线程和非线程函数调用的执行顺序是什么?

C++ Win32 窗口无响应

c++ - 是否可以在编译时获取时间(当天)和日期?

python - Tensorflow reshape 张量

numpy - 使用自定义函数迭代和累积 numpy 数组

python - 有没有办法根据子数组对张量进行排序?

python - 如何使用 tf.train.Checkpoint 保存大量变量