【问题标题】:How do I write CMakeList.txt file to include the tensorflow library? (c++ build)如何编写 CMakeList.txt 文件以包含 tensorflow 库? (c++ 构建)
【发布时间】:2020-12-14 08:10:04
【问题描述】:

我已经用 bazel 成功构建了 tensorflow(v1.14.0) c++。 我可以用 g++ 构建 tensorflow 库。

我想在此代码中包含其他库(例如 json)。 所以我想知道如何用cmake构建下面的代码。

CMakeLists.txt怎么写?

我的目录如下。

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

#include <chrono>
#include <stream>
#include <string>
#include <vector>
#include <stream>

#include <stream>
#include <list>
#include <memory>

using namespace std;
using namespace chrono;
using namespace tensorflow;

int main(int argc, char* argv[]) {

  // Initialize a tensorflow session
  cout << "start initalize session" << "\n";
  Session* session;
  Status status = NewSession(SessionOptions(), &session);
  if (!status.ok()) {
    cout << status.ToString() << "\n";
    return 1;
  }
  ...

g++ -std=c++11 -Wl,-rpath=lib -Iinclude -Llib -ltensorflow_framework test.cpp -ltensorflow_cc -ltensorflow_framework -o exec

【问题讨论】:

    标签: c++ tensorflow cmake g++ tensorflow-c++


    【解决方案1】:

    要创建可用作 CMake 构建脚本的纯文本文件,请执行以下操作:

    从 IDE 左侧打开“项目”窗格,然后从下拉菜单中选择“项目”视图。 右键单击模块的根目录,然后选择新建 > 文件。

    注意:您可以在任何您想要的位置创建构建脚本。但是,在配置构建脚本时,本地源文件和库的路径是相对于构建脚本的位置的。

    输入“CMakeLists.txt”作为文件名,然后单击确定。

    要将源文件或库添加到 CMake 构建脚本,请使用 add_library():

    add_library(...)
    
    # Specifies a path to native header files.
    include_directories(src/main/cpp/include/)
    

    CMake 用于命名库文件的约定如下:

    liblibrary-name.so
    

    这是official documentation的链接。

    【讨论】:

      【解决方案2】:
      cmake_minimum_required(VERSION 3.16)
      project(PROJECT_NAME)
      find_library(TENSORFLOW_LIB tensorflow HINT <path>/lib)
      
      set(CMAKE_CXX_STANDARD 14)
      
      add_executable(${PROJECT_NAME} main.cpp)
      
      target_include_directories(${PROJECT_NAME} PRIVATE <path>/include)
      
      target_link_libraries (${PROJECT_NAME} "${TENSORFLOW_LIB}" -ltensorflow)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-04
        • 2021-04-25
        • 1970-01-01
        • 1970-01-01
        • 2021-11-22
        • 2013-01-06
        • 1970-01-01
        相关资源
        最近更新 更多