Running arbitrary command line C++ programs on Android using CMake from the official NDK

Balasaravanan Venugopal

Let us take a simple example C++ program like the one below

#include <iostream>
using namespace std;
int main ()
cout << "Hello world from C++ \n";
return 0;

The CMakeLists.txt file contents are as below

cmake_minimum_required (VERSION 2.6)
project (android_app_01)
add_executable(android_app_01 test_app.cpp)

To compile the above-said program for Android install the Android SDK with NDK components. Then invoke the make program using the following flags

/home/cm-dev/Android/Sdk/cmake/ -DANDROID_ABI=armeabi-v7a -DANDROID_NDK=/home/bala/Android/Sdk/android-ndk-r17c -
DCMAKE_TOOLCHAIN_FILE=/home/cm-dev/Android/Sdk/android-ndk-r17c/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=23 -DANDROID_TOOLCHAIN=gcc ../

Note that the GCC toolchain will be removed from the r19 version of the NDK. The compiled binary can then be moved into the /data/local/tmp folder on the Android device. Any other folder on the device will not be writable. The files can be moved onto the Android device using conventional adb commands.

cd Android/Sdk/platform-tools/
./adb push /home/bala/cm/99_tryout/compile_android_01/build/android_app_02 /data/local/tmp
./adb shell

If the C++ program uses 3rd party dependency libraries, it is essential to setup the library paths. Before running an executable on ADB shell we need to add LD_LIBRARY_PATH

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:current_directory

Once on the ADB shell and move the data tmp directory and chmod the binary as executable. You should then be able to run the program from the shell

chmod 755 android_app_02

Get our blogs delivered

From us to your inbox weekly

Related Articles

Get a free quote for your next project

We can help you build cloud and data products faster. Our Microsoft certified professionals can help you in driving business value with exceptional quality.

Contact us