김기훈
김기훈

Reputation: 31

How do I write CMakeList.txt file to include the tensorflow library? (c++ build)

I have succeeded build tensorflow(v1.14.0) c++ with bazel. And I can build tensorflow library with g++.

I want to include other libraries (eg json) in this code. So I want to know how to build the code below with cmake.

How to write CMakeLists.txt?

My directory is as follows.

enter image description here

< test.cpp code >

#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++ command >

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

Upvotes: 0

Views: 754

Answers (2)

Saurav Solanki
Saurav Solanki

Reputation: 309

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)

Upvotes: 0

Alex Zeynalov
Alex Zeynalov

Reputation: 24

To create a plain text file that you can use as your CMake build script, proceed as follows:

Open the Project pane from the left side of the IDE and select the Project view from the drop-down menu. Right-click on the root directory of your-module and select New > File.

Note: You can create the build script in any location you want. However, when configuring the build script, paths to your native source files and libraries are relative to the location of the build script.

Enter "CMakeLists.txt" as the filename and click OK.

To add a source file or library to your CMake build script use add_library():

add_library(...)

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

The convention CMake uses to name the file of your library is as follows:

liblibrary-name.so

And here's the link to the official documentation.

Upvotes: 0

Related Questions