user2936448
user2936448

Reputation: 339

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.

Upvotes: 5

Views: 5112

Answers (2)

Sergio
Sergio

Reputation: 8209

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.

Upvotes: 4

antlersoft
antlersoft

Reputation: 14786

I think the key statement is found on this page: UCI and XBoard Engines for Android

For x86-based Android devices (such as Google TV), any engine binary that has been compiled for 32-bit x86 Linux will work. This format is widely available for many chess engines.

Which means that Chess for Android can use engines exactly as if they were compiled for Linux. This isn't too surprising, since Android is based on Linux and has the native Linux APIs available. Chess for Android is using these APIs to run the engines as normal Linux native executables.

So if you have a 32-bit Linux x86 system and your Android device is an x86 device, just compile your engine on your Linux system as if you were using it locally and install it on your device according to the instructions on the page you linked.

However, your Android device is probably not an x86 device, it is probably an ARM device. In that case, you need a cross-compiler running on your computer that can compile it to a Linux executable targeting ARM (this is described briefly in the part you quoted). The cross compiler will compile your code the same way a native compiler would, but the resulting executable will have machine instructions for the ARM chip rather than the x86 chip you have in your computer.

Since you are (presumably) developing on Windows, it will probably be most convenient to use the Android NDK cross-compiler. Install it according to its instructions (note that most of the instructions for actually using the NDK aren't relevant for you, since you are not creating native libraries for an Android application, you are creating an ordinary Linux executable)

The NDK includes a gcc cross-compiler tool chain. You will use this to build your native .exe. Make sure your PATH contains the folder in the NDK containing the gcc compiler front-end. Then if your engine source code is in a file call engine.cc, you should be able to compile it to an ARM binary with a command like

gcc -o MyUCIEngine engine.cc

You may have to set some more environment variables and the actual command to do the build will be more complex. This page has the additional information you need for running the cross-compiling tool chain in the NDK.

Upvotes: 0

Related Questions