要编译原生的Android二进制我可以在终端手机上运行 [英] Want to compile native Android binary I can run in terminal on the phone
问题描述
我一直试图几天来编译原生ARM的Android二进制将使用终端应用程序在手机上运行。我想生成相同的二进制类型为安装在手机ls之类,MKDIR等。我已经下载在Mac OS X中的Android NDK,并已能没有错误编译简单的ELF可执行文件,在标准的POSIX二进制文件。然而,当我将它们传输到手机上,他们总是出现段错误。也就是说,它们段错误的时候用-static在GCC编译。如果我不使用-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使用的仿生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一个不允许我编机二进制文件,我可以通过终端执行?这里任何指导,大大AP preciated!
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!
更新:
我终于得到这个使用GCC 4.7.0在Mac OS X我下载了仿生头工作,然后用自带的Android NDK的C库编译动态链接的二进制文件。我能得到一个测试应用程序的工作使用电话公司的C LIB(二进制是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海湾合作委员会的命令行。它的工作原理是这样的:
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.
在这个实验中,我只用简单的'醇GCC 4.7.0和Binutils 2.22。我也GCC编译与newlib,但我实际上没有链接我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像这样。
包括$(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屋!