Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you compile a C++ console program to run as an android process?

I wrote a chess engine in C++ which I want to compile to run on the app "Chess for Android".

Here is an excerpt from a post from the author of the app who very very crudely describes how to run a 3rd party chess engine on his app:

Finally, developers can do all development in C/C++ and generate stand-alone native code using the appropriate compiler toolchain (e.g. CodeSourcery for ARM or the toolchain that ships with the NDK). This third approach is used by Chess for Android to import engines that do not ship with the application.

A chess engine is a straightforward program. The application runs, the user sends it commands, the program thinks, and will spit back a string with the details of the best move.

I do NOT need an app with graphics or anything else. it's ONLY a process which is communicated with via stdin/stdout (via pipes really). The "Chess for Android" app takes care of talking to the process, I just need to figure out how in the world to get my engine as a process that can be run by the "Chess for Android" app.

like image 831
user2936448 Avatar asked Jun 22 '16 19:06

user2936448


People also ask

How can I convert my C application to Android app?

By Using C language we can create android app, for that you need to use Android NDK development. There you can write code in C or C++ language. It will convert that . so file or JNI to run as an Android application.

Can I run C program in Android?

Download the NDK and build toolsThe Android Native Development Kit (NDK): a toolset that allows you to use C and C++ code with Android, and provides platform libraries that allow you to manage native activities and access physical device components, such as sensors and touch input.

How do I open a .C file on Android?

To Install and Use C/C++ compiler in Termux (in Termux clang is the C/C++ compiler) , Download & Install Termux from : Play Store. After Installing execute this command pkg install clang. After Successfully installing clang you can compile C/C++ scripts.

Can C++ compile to Android?

Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI) framework.


1 Answers

You can build traditional console program for Android in two ways.

  1. Using of ndk-build system.

You need to create two extra files: Application.mk:

APP_ABI := armeabi-v7a
APP_PLATFORM := android-16
APP_STL := stlport_static
NDK_TOOLCHAIN_VERSION := 4.8

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := <executable_name>
LOCAL_LDLIBS := -llog -lm # whatever libs you need
LOCAL_CFLAGS := -O3 -UNDEBUG # so on
LOCAL_C_INCLUDES := <list of a non-standard header directories>
LOCAL_SRC_FILES := <sources>
include $(BUILD_EXECUTABLE) # build executable instead of library

The former describes target ABIs, target android level, STL implementation and selects build toolchain. The latter is itself a simpe makefile, and it shouldn't be confusing. See here for detais Then you can start build with next command:

$ ndk-build NDK_PROJECT_PATH=path/to/intermediate/results/dir NDK_LIBS_OUT=path/to/output/dir APP_BUILD_SCRIPT=path/to/Android.mk NDK_APPLICATION_MK=path/to/Application.mk

Also, note that ndk-build adds some flags by default. To check it - add V=1 to above command line.

  1. Using of standalone toolchain.

This approach may be useful if you already have build system for your project and you need only cross-compiler. To prepare standalone toochain run e.g next:

$NDK/build/tools/make-standalone-toolchain.sh --arch=arm --platform=android-21 --install-dir=/tmp/my-android-toolchain

And now export path to tools:

$ export PATH=/tmp/my-android-toolchain/bin:$PATH
$ export CC=arm-linux-androideabi-gcc
$ export CXX=arm-linux-androideabi-g++

More info here.

like image 88
Sergio Avatar answered Oct 11 '22 12:10

Sergio