想要编译本机 Android 二进制文件,我可以在手机的终端中运行 [英] Want to compile native Android binary I can run in terminal on the phone
问题描述
几天以来,我一直在尝试编译一个本地 ARM Android 二进制文件,该二进制文件将使用终端应用程序在我的手机上执行.我想生成与手机上安装的标准 Posix 二进制文件相同类型的二进制文件,如 ls、mkdir 等.我已经在 Mac OS X 下下载了 Android NDK,并且能够编译简单的 ELF 二进制文件而不会出错.但是,当我将它们传输到手机时,它们总是出现段错误.也就是说,它们在 GCC 中使用 -static 编译时会出现段错误.如果我不使用 -static,他们会抱怨没有被链接等等.简而言之,他们不起作用.
I've been trying for a couple days to compile a native ARM Android binary that will execute on my phone using a terminal application. I want to generate the same type of binary as the standard Posix binaries installed on the phone like ls, mkdir etc. I've downloaded the Android NDK under Mac OS X and have been able to compile simple ELF binaries without errors. However, when I transfer them to the phone, they always segfault. That is, they segfault when compiled with -static in GCC. If I don't use -static, they complain about not being linked, etc. Put simply, they don't work.
我的假设是它们没有正确链接到 Android 标准 C 库.即使我将我的二进制文件与 NDK 提供的 libc 链接起来,它们仍然不起作用.我读到 Android 使用 Bionic C 库,并尝试下载它的源代码,但我不确定如何从中构建库(似乎都是 ARM 程序集).
My hypothesis is that they are not linking to the Android standard C library properly. Even though I am linking my binaries with the libc provided by the NDK, they still don't work. I read that Android uses the Bionic C library, and tried to download source for it but I'm not sure how to build a library from it (it's all ARM assembly, it seems).
手机上的Android C库和Android NDK自带的库是不是真的?NDK 中包含的那个不允许我编译可以通过终端执行的本机二进制文件吗?非常感谢这里的任何指导!
Is it true that the Android C library on the phone is different from the one provided with the Android NDK? Will the one included with the NDK not allow me to compile native binaries I can execute through a terminal? Any guidance here is greatly appreciated!
更新:
我终于在 Mac OS X 上使用 GCC 4.7.0 让它工作了.我下载了 Bionic 头文件,然后使用 Android NDK 附带的 C 库编译了一个动态链接的二进制文件.我能够使用手机的 C 库(二进制文件为 33K)让测试应用程序在手机上运行.我还尝试静态链接 NDK 的 C 库,这也有效.
I finally got this to work using GCC 4.7.0 on Mac OS X. I downloaded the Bionic headers and then compiled a dynamically linked binary using the C library that comes with the Android NDK. I was able to get a test app to work on the phone using the phone's C lib (the binary was 33K). I also tried to statically link against the NDK's C library, and that also worked.
为了让这一切正常工作,我必须将 -nostdlib 传递给 GCC,然后手动将 crtbegin_dynamic.o 和 crtend_android.o 添加到 GCC 的命令行.它的工作原理是这样的:
In order to get this all working I had to pass -nostdlib to GCC and then manually add crtbegin_dynamic.o and crtend_android.o to GCC's command line. It works something like this:
$CC \
$NDK_PATH/usr/lib/crtbegin_dynamic.o \
hello.c -o hello \
$CFLAGS \
$NDK_PATH/usr/lib/crtend_android.o
对于静态二进制文件,使用crtbegin_static.o".这在 crtbegin_dynamic.S/crtbegin_static.S 源中有解释.
For static binaries, use "crtbegin_static.o." This is explained in the crtbegin_dynamic.S/crtbegin_static.S source.
对于这个实验,我只使用了普通的 'ol GCC 4.7.0 和 Binutils 2.22.我还用 newlib 编译了 GCC,但我实际上并没有将我的 ARM 二进制文件与 newlib 链接起来.我强制 GCC/ld 直接链接到 Android NDK 提供的 libc,或者在动态二进制文件的情况下,链接到手机上的 libc.
For this experiment, I only used plain 'ol GCC 4.7.0 and Binutils 2.22. I also compiled GCC with newlib, but I am not actually linking my ARM binaries with newlib at all. I am forcing GCC/ld to link directly to the libc provided with the Android NDK, or in the case of dynamic binaries, to the libc on the phone.
推荐答案
只需使用 android-ndk.并像这样构建一个 Android.mk.include $(BUILD_EXECUTABLE)
告诉它构建可执行文件而不是 JNI .lib
Just use the android-ndk. And build a Android.mk like so.
include $(BUILD_EXECUTABLE)
is what tells it build a executable instead of a JNI .lib
ifneq ($(TARGET_SIMULATOR),true)
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CFLAGS += -Wall
LOCAL_LDLIBS := -L$(LOCAL_PATH)/lib -llog -g
LOCAL_C_INCLUDES := bionic
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_SRC_FILES:= main.cpp
LOCAL_MODULE := mycmd
include $(BUILD_EXECUTABLE)
endif # TARGET_SIMULATOR != true
这篇关于想要编译本机 Android 二进制文件,我可以在手机的终端中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!