Vujadeyoon
Vujadeyoon

Reputation: 21

How to make CMakeLists.txt files for LibTensorFlow

I wonder how to make CMakeLists.txt files for Install TensorFlow for C.

I can build the C++ file (i.e. ./app/main.cpp) with g++, but it is difficult to build the same C++ file with CMake file. Please note that the directory structure is shown in [1].

The main.cpp file is as below:

#include <iostream>
#include <tensorflow/c/c_api.h>

int main() {
  std::cout << "LibTensorFlow Version: " << TF_Version() << std::endl;

  return 0;
}

The g++ command is as below:

$ path_curr=$(pwd)
$ path_libtf=${path_curr}/lib_ext/libtensorflow_cpu
$ export LIBRARY_PATH=$LIBRARY_PATH:${path_libtf}/lib
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${path_libtf}/lib
$ g++ -o ./main ./app/main.cpp \
      -I${path_libtf}/include -L${path_libtf}/lib -ltensorflow

The CMakeLists.txt files are as below:

# cat ./CMakeLists.txt

cmake_minimum_required(VERSION 3.11)

project(
  SampleLibTensorFlow
  VERSION 0.1
  DESCRIPTION "Sample code for LibTensorFlow"
  LANGUAGES CXX)

# Set the path for executable files.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

add_subdirectory(app)

# cat ./app/CMakeLists.txt

# Add all cpp files in this directory into the variable, SRC_FILES.
file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS
  ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)

add_executable(program ${SRC_FILES})

# LibTensorFlow
find_library(tensorflow
             NAMES tensorflow TensorFlow libtensorflow LibTensorFlow
             HINTS /home/vujadeyoon/Desktop/libtensorflow-cmake/lib_ext/libtensorflow_cpu/lib/*.so
             REQUIRED
             )
target_link_libraries(program PUBLIC tensorflow)

When I run below commands to build it, I encounter some errors as follows:

$ cd build
$ rm -rf * && cmake .. && make -j8 && ./bin/program
-- The CXX compiler identification is GNU 9.4.0
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/vujadeyoon/Desktop/libtensorflow-cmake/build
    Scanning dependencies of target program
    [ 50%] Building CXX object app/CMakeFiles/program.dir/main.cpp.o
    /home/vujadeyoon/Desktop/libtensorflow-cmake/app/main.cpp:2:10: fatal error: tensorflow/c/c_api.h: No such file or directory
        2 | #include <tensorflow/c/c_api.h>
          |          ^~~~~~~~~~~~~~~~~~~~~~
    compilation terminated.
    make[2]: *** [app/CMakeFiles/program.dir/build.make:63: app/CMakeFiles/program.dir/main.cpp.o] Error 1
    make[1]: *** [CMakeFiles/Makefile2:94: app/CMakeFiles/program.dir/all] Error 2
    make: *** [Makefile:84: all] Error 2

I don't know how to modify the CMakeLists.txt files. Could you please explain how to modify them?

You can check the full code directory in LibTensorFlow.

[1] The directory structure

LibTensorFlow-Cmake
├── app
│   ├── CMakeLists.txt
│   └── main.cpp
├── _arxiv
│   └── libtensorflow-cpu-linux-x86_64-2.8.0.tar.gz
├── build
├── CMakeLists.txt
├── lib_ext
│   └── libtensorflow_cpu
│       ├── include
│       │   └── tensorflow
│       │       ├── c
│       │       │   ├── c_api_experimental.h
│       │       │   ├── c_api.h
│       │       │   ├── c_api_macros.h
│       │       │   ├── eager
│       │       │   │   ├── c_api_experimental.h
│       │       │   │   ├── c_api.h
│       │       │   │   └── dlpack.h
│       │       │   ├── tensor_interface.h
│       │       │   ├── tf_attrtype.h
│       │       │   ├── tf_datatype.h
│       │       │   ├── tf_file_statistics.h
│       │       │   ├── tf_status.h
│       │       │   ├── tf_tensor.h
│       │       │   └── tf_tstring.h
│       │       └── core
│       │           └── platform
│       │               ├── ctstring.h
│       │               └── ctstring_internal.h
│       ├── lib
│       │   ├── libtensorflow_framework.so -> libtensorflow_framework.so.2
│       │   ├── libtensorflow_framework.so.2 -> libtensorflow_framework.so.2.8.0
│       │   ├── libtensorflow_framework.so.2.8.0
│       │   ├── libtensorflow.so -> libtensorflow.so.2
│       │   ├── libtensorflow.so.2 -> libtensorflow.so.2.8.0
│       │   └── libtensorflow.so.2.8.0
│       ├── LICENSE
│       └── THIRD_PARTY_TF_C_LICENSES
├── LICENSE
├── main
├── README.md
└── script
    ├── bash_install_libtf.sh
    └── bash_run.sh

Upvotes: 0

Views: 295

Answers (1)

Vujadeyoon
Vujadeyoon

Reputation: 21

I succeeded in building the above C++ file with CMake.

I modify the ./app/CMakeLists.txt file as follows:

# cat ./app/CMakeLists.txt

# Add all cpp files in this directory into the variable, SRC_FILES.
file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS
  ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)

add_executable(program ${SRC_FILES})

# LibTensorFlow
target_include_directories(program PUBLIC ${CMAKE_SOURCE_DIR}/lib_ext/libtensorflow_cpu/include)
target_link_libraries(program tensorflow)

After modifying the CMakeLists.txt file, I run the below codes to build it and confirm that the ./app/main.cpp is correctly built.

$ path_curr=$(pwd)
$ path_libtf=${path_curr}/lib_ext/libtensorflow_cpu
$ export LIBRARY_PATH=$LIBRARY_PATH:${path_libtf}/lib
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${path_libtf}/lib
$ rm -rf * && cmake .. && make -j8 && ./bin/program
-- The CXX compiler identification is GNU 9.4.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/vujadeyoon/Desktop/libtensorflow-cmake/build
Scanning dependencies of target program
[ 50%] Building CXX object app/CMakeFiles/program.dir/main.cpp.o
[100%] Linking CXX executable ../bin/program
[100%] Built target program
LibTensorFlow Version: 2.8.0-dev20220131

Upvotes: 0

Related Questions