title: How to make CMakeLists.txt files for LibTensorFlow
body: <p>I wonder how to make CMakeLists.txt files for <a href="https://www.tensorflow.org/install/lang_c" rel="nofollow noreferrer">Install TensorFlow for C</a>.</p>
  <p>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].</p>
  <p>The main.cpp file is as below:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;tensorflow/c/c_api.h&gt;
  
  int main() {
    std::cout &lt;&lt; &quot;LibTensorFlow Version: &quot; &lt;&lt; TF_Version() &lt;&lt; std::endl;
  
    return 0;
  }
  
  </code></pre>
  <p>The g++ command is as below:</p>
  <pre class="lang-bash prettyprint-override"><code>$ 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
  </code></pre>
  <p>The CMakeLists.txt files are as below:</p>
  <pre class="lang-bash prettyprint-override"><code># cat ./CMakeLists.txt
  
  cmake_minimum_required(VERSION 3.11)
  
  project(
    SampleLibTensorFlow
    VERSION 0.1
    DESCRIPTION &quot;Sample code for LibTensorFlow&quot;
    LANGUAGES CXX)
  
  # Set the path for executable files.
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  
  add_subdirectory(app)
  
  </code></pre>
  <pre class="lang-bash prettyprint-override"><code># 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)
  
  </code></pre>
  <p>When I run below commands to build it, I encounter some errors as follows:</p>
  <pre class="lang-bash prettyprint-override"><code>$ cd build
  $ rm -rf * &amp;&amp; cmake .. &amp;&amp; make -j8 &amp;&amp; ./bin/program
  </code></pre>
  <pre class="lang-bash prettyprint-override"><code>-- 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 &lt;tensorflow/c/c_api.h&gt;
            |          ^~~~~~~~~~~~~~~~~~~~~~
      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
  </code></pre>
  <p>I don't know how to modify the CMakeLists.txt files.
  Could you please explain how to modify them?</p>
  <p>You can check the full code directory in <a href="https://github.com/vujadeyoon/LibTensorFlow" rel="nofollow noreferrer">LibTensorFlow</a>.</p>
  <p>[1] The directory structure</p>
  <pre class="lang-bash prettyprint-override"><code>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 -&gt; libtensorflow_framework.so.2
  │       │   ├── libtensorflow_framework.so.2 -&gt; libtensorflow_framework.so.2.8.0
  │       │   ├── libtensorflow_framework.so.2.8.0
  │       │   ├── libtensorflow.so -&gt; libtensorflow.so.2
  │       │   ├── libtensorflow.so.2 -&gt; 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
  
  </code></pre>