android - 如何编译 C++ 控制台程序以作为 android 进程运行?

标签 android c++ windows android-ndk

我用 C++ 编写了一个国际象棋引擎,我想编译它以在应用程序“Chess for Android”上运行。

这是应用程序作者 very very crudely 的帖子的摘录描述了如何在他的应用程序上运行第 3 方国际象棋引擎:

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.

国际象棋引擎是一个简单的程序。应用程序运行,用户向它发送命令,程序思考,然后吐出包含最佳着法细节的字符串。

我不需要带有图形或其他任何东西的应用程序。它只是一个通过标准输入/标准输出(实际上是通过管道)与之通信的进程。 “Android 国际象棋”应用程序负责与进程对话,我只需要弄清楚如何让我的引擎成为一个可以由“Android 国际象棋”应用程序运行的进程。

最佳答案

您可以通过两种方式为 Android 构建传统的控制台程序。

  1. 使用 ndk-build 系统。

您需要创建两个额外的文件: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

前者描述目标 ABI、目标 android 级别、STL 实现并选择构建工具链。后者本身就是一个 simpe makefile,不应引起混淆。参见 here详情 然后你可以使用下一个命令开始构建:

$ 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

另请注意,ndk-build 默认添加了一些标志。要检查它 - 添加 V=1 到上面的命令行。

  1. 使用独立工具链。

如果您的项目已经有了构建系统并且您只需要交叉编译器,这种方法可能会有用。准备独立的 toochain 运行,例如:

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

现在将路径导出到工具:

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

更多信息 here .

关于android - 如何编译 C++ 控制台程序以作为 android 进程运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37976396/

相关文章:

windows - PowerShell:按可执行名称重启服务

c - 如何获得正确的显示器物理尺寸?

android - 在键盘上方设置 FAB( float 操作按钮)

android - 使用数据绑定(bind)时导航栏出现 2 次

c++ - 如何在类中创建模板函数? (C++)

c++ - 结构与 vector 作为数据持有者

c++ - 正则表达式+编译 boost ?

java - 如何检查应用程序是否正在打开 - Android Studio

android - 为什么带有 Chip 的 marginEnd 的 Horizo​​ntalScrollView 中的 ChipGroup 不起作用?

c++ - C++ 中 HTTP URL 的 #include