title: How to use cmake from command line to make an executable for rooted Android device?
body: <p>I want to run a simple executable that should print "Hello Cmake" when I will execute it from adb shell. For this, I have created a simple c++ file with CMakeLists.txt file as following:</p>
  
  <p><strong>hello.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main(int, char**) {
      std::cout &lt;&lt; "Hello, CMake!\n";
  }
  
  </code></pre>
  
  <p><strong>CMakeLists.txt</strong></p>
  
  <pre><code>
  cmake_minimum_required(VERSION 3.4.1)
  
  add_library( # Sets the name of the library.
               hello_cmake
  
               # Sets the library as a static library.
               STATIC
  
               # Provides a relative path to your source file(s).
               hello.cpp )
  </code></pre>
  
  <p>I have tried to run following command in terminal:</p>
  
  <pre><code>cmake D:/Development/CMAKE/HelloCmake/ -G Ninja \
  -DANDROID_TOOLCHAIN_NAME=aarch64-linux-android29-clang++ \
  -DANDROID_PLATFORM=29 \
  -DCMAKE_CACHEFILE_DIR=D:/Development/CMAKE/HelloCmake/build \
  -DCMAKE_MAKE_PROGRAM=D:/Sdk/cmake/3.10.2.4988404/bin/ninja.exe \
  -DCMAKE_C_COMPILER=D:/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android29-clang \
  -DCMAKE_CXX_COMPILER=D:/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android29-clang++
  </code></pre>
  
  <p>This has the following result:</p>
  
  <pre><code>D:\Development\CMAKE\HelloCmake&gt;cmake D:/Development/CMAKE/HelloCmake/ -G Ninja -DANDROID_TOOLCHAIN_NAME=aarch64-linux-android29-clang++ -DANDROID_PLATFORM=29 -DCMAKE_CACHEFILE_DIR=D:/Development/CMAKE/HelloCmake/build -DCMAKE_MAKE_PROGRAM=D:/Sdk/cmake/3.10.2.4988404/bin/ninja.exe -DCMAKE_C_COMPILER=D:/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android29-clang -DCMAKE_CXX_COMPILER=D:/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android29-clang++
  -- The C compiler identification is unknown
  -- The CXX compiler identification is unknown
  -- Check for working C compiler: D:/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android29-clang
  -- Check for working C compiler: D:/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android29-clang -- broken
  CMake Error at D:/Sdk/cmake/3.10.2.4988404/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake:52 (message):
    The C compiler
  
      "D:/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android29-clang"
  
    is not able to compile a simple test program.
  
    It fails with the following output:
  
      Change Dir: D:/Development/CMAKE/HelloCmake/CMakeFiles/CMakeTmp
  
      Run Build Command:"D:/Sdk/cmake/3.10.2.4988404/bin/ninja.exe" "cmTC_e4775"
      ninja: fatal: CreateProcess: %1 is not a valid Win32 application.
  
  
  
  
  
    CMake will not be able to correctly generate this project.
  Call Stack (most recent call first):
    CMakeLists.txt
  
  
  -- Configuring incomplete, errors occurred!
  See also "D:/Development/CMAKE/HelloCmake/CMakeFiles/CMakeOutput.log".
  See also "D:/Development/CMAKE/HelloCmake/CMakeFiles/CMakeError.log".
  
  D:\Development\CMAKE\HelloCmake&gt;
  </code></pre>
  
  <p>Please help me to configure it correctly.</p>
  
  <p><strong>Update 1</strong>
  With Michael guidance, I have found the <code>build_command.txt</code> file in Android Studio and it has the following contents for simple "Hello World" application:</p>
  
  <pre><code>Executable : D:\Sdk\cmake\3.10.2.4988404\bin\cmake.exe
  arguments : 
  -HD:\Development\Android\HelloCPP\app\src\main\cpp
  -BD:\Development\Android\HelloCPP\app\.cxx\cmake\debug\arm64-v8a
  -DANDROID_ABI=arm64-v8a
  -DANDROID_PLATFORM=android-26
  -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=D:\Development\Android\HelloCPP\app\build\intermediates\cmake\debug\obj\arm64-v8a
  -DCMAKE_BUILD_TYPE=Debug
  -DANDROID_NDK=D:\Sdk\ndk\20.1.5948944
  -DCMAKE_CXX_FLAGS=-std=c++14
  -DCMAKE_SYSTEM_NAME=Android
  -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a
  -DCMAKE_SYSTEM_VERSION=26
  -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
  -DCMAKE_ANDROID_NDK=D:\Sdk\ndk\20.1.5948944
  -DCMAKE_TOOLCHAIN_FILE=D:\Sdk\ndk\20.1.5948944\build\cmake\android.toolchain.cmake
  -G Ninja
  -DCMAKE_MAKE_PROGRAM=D:\Sdk\cmake\3.10.2.4988404\bin\ninja.exe
  jvmArgs : 
  </code></pre>
  
  <p>I have run the following command for my application:</p>
  
  <pre><code>D:\Sdk\cmake\3.10.2.4988404\bin\cmake.exe 
  -HD:\Development\CMAKE\HelloCmake\ 
  -BD:\Development\CMAKE\HelloCmake\arm64-v8a 
  -DANDROID_ABI=arm64-v8a 
  -DANDROID_PLATFORM=android-29 
  -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=D:\Development\CMAKE\HelloCmake\build 
  -DCMAKE_BUILD_TYPE=Debug 
  -DANDROID_NDK=D:\Sdk\ndk\20.1.5948944 
  -DCMAKE_CXX_FLAGS=-std=c++14 
  -DCMAKE_SYSTEM_NAME=Android 
  -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a 
  -DCMAKE_SYSTEM_VERSION=26 
  -DCMAKE_EXPORT_COMPILE_COMMANDS=ON 
  -DCMAKE_ANDROID_NDK=D:\Sdk\ndk\20.1.5948944 
  -DCMAKE_TOOLCHAIN_FILE=D:\Sdk\ndk\20.1.5948944\build\cmake\android.toolchain.cmake 
  -G Ninja 
  -DCMAKE_MAKE_PROGRAM=D:\Sdk\cmake\3.10.2.4988404\bin\ninja.exe
  </code></pre>
  
  <p>and I get the following output:</p>
  
  <pre><code>D:\Development\CMAKE\HelloCmake&gt;D:\Sdk\cmake\3.10.2.4988404\bin\cmake.exe -HD:\Development\CMAKE\HelloCmake\ -BD:\Development\CMAKE\HelloCmake\arm64-v8a -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-29 -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=D:\Development\CMAKE\HelloCmake\build -DCMAKE_BUILD_TYPE=Debug -DANDROID_NDK=D:\Sdk\ndk\20.1.5948944 -DCMAKE_CXX_FLAGS=-std=c++14 -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a -DCMAKE_SYSTEM_VERSION=26 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_ANDROID_NDK=D:\Sdk\ndk\20.1.5948944 -DCMAKE_TOOLCHAIN_FILE=D:\Sdk\ndk\20.1.5948944\build\cmake\android.toolchain.cmake -G Ninja -DCMAKE_MAKE_PROGRAM=D:\Sdk\cmake\3.10.2.4988404\bin\ninja.exe
  -- Check for working C compiler: D:/Sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
  -- Check for working C compiler: D:/Sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- works
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - done
  -- Detecting C compile features
  -- Detecting C compile features - done
  -- Check for working CXX compiler: D:/Sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe
  -- Check for working CXX compiler: D:/Sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe -- 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: D:/Development/CMAKE/HelloCmake/arm64-v8a
  
  D:\Development\CMAKE\HelloCmake&gt;
  </code></pre>
  
  <p>Build files are written to <code>arm64-v8a</code> but I didn't find any <code>ELF shared object, 64-bit LSB arm64, dynamic (/system/bin/linker64), stripped</code> object that should be able to run on my android device.</p>
  
  <p>I have found only the following files:</p>
  
  <pre><code>$ find . | xargs file | grep ELF
  ./CMakeFiles/3.10.2/CMakeDetermineCompilerABI_C.bin:   ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[sha1]=7cb1fddcd4776716628feaf37d471c1ea4a55314, with debug_info, not stripped
  ./CMakeFiles/3.10.2/CMakeDetermineCompilerABI_CXX.bin: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[sha1]=1f498297f62e5a52751312894e88a9abef0412d5, with debug_info, not stripped
  ./CMakeFiles/feature_tests.bin:                        ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[sha1]=a56baeb98e3f077c3cc0a512b0535089a717929c, with debug_info, not stripped
  </code></pre>
  
  <p>Any suggestion?</p>
  
  <p><strong>Update 2</strong>
  I have run <code>D:\Sdk\cmake\3.10.2.4988404\bin\ninja.exe -C arm64-v8a</code> command to make the target static library but I get <code>libhello_cmake.a</code> which is <code>current ar archive</code>.</p>
  
  <p>I think to make an executable the <code>CMakeLists.txt</code> <code>add_library</code> line should be replaced with something else?</p>