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.
我已经尝试了几天来编译一个本机ARM Android二进制文件,它将使用终端应用程序在我的手机上执行。我想生成与手机上安装的标准Posix二进制文件相同类型的二进制文件,如ls,mkdir等。我已经在Mac OS X下载了Android NDK,并且能够无错误地编译简单的ELF二进制文件。但是,当我将它们转移到手机时,它们总是会出现段错误。也就是说,在GCC中使用-static编译时会出现段错误。如果我不使用-static,他们会抱怨没有链接等等。简单地说,它们不起作用。
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库。即使我将我的二进制文件与NDK提供的libc链接,它们仍然无法正常工作。我读到Android使用Bionic C库,并试图为它下载源代码,但我不确定如何从它构建一个库(它似乎是所有ARM程序集)。
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!
手机上的Android C库是否与Android NDK提供的库不同?包含在NDK中的那个不允许我编译我可以通过终端执行的本机二进制文件吗?非常感谢任何指导!
Update:
更新:
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.
我终于在Mac OS X上使用GCC 4.7.0了解它。我下载了Bionic标头,然后使用Android NDK附带的C库编译了一个动态链接的二进制文件。我能够使用手机的C lib(二进制为33K)获得一个测试应用程序在手机上工作。我还尝试静态链接NDK的C库,这也有效。
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:
为了使这一切工作,我必须将-nostdlib传递给GCC,然后手动将crtbegin_dynamic.o和crtend_android.o添加到GCC的命令行。它的工作原理如下:
$CC \
$NDK_PATH/usr/lib/crtbegin_dynamic.o \
hello.c -o hello \
$CFLAGS \
$NDK_PATH/usr/lib/crtend_android.o
For static binaries, use "crtbegin_static.o." This is explained in the crtbegin_dynamic.S/crtbegin_static.S source.
对于静态二进制文件,请使用“crtbegin_static.o”。这在crtbegin_dynamic.S / crtbegin_static.S源中进行了解释。
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.
对于这个实验,我只使用普通的'ol GCC 4.7.0和Binutils 2.22。我还使用newlib编译了GCC,但我实际上并没有将我的ARM二进制文件与newlib链接起来。我强迫GCC / ld直接链接到随Android NDK提供的libc,或者在动态二进制文件的情况下,链接到手机上的libc。
4 个解决方案
#1
19
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
只需使用android-ndk。并像这样构建一个Android.mk。 include $(BUILD_EXECUTABLE)告诉它构建一个可执行文件而不是JNI .lib
Android.mk
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
#2
15
First, make sure you have the NDK:
首先,确保你有NDK:
http://developer.android.com/tools/sdk/ndk/index.html
http://developer.android.com/tools/sdk/ndk/index.html
Here is the easiest way to compile a C binary for your phone:
这是为手机编译C二进制文件的最简单方法:
http://developer.android.com/tools/sdk/ndk/index.html
http://developer.android.com/tools/sdk/ndk/index.html
http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html
http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html
Usually $NDK(may be different) =
通常$ NDK(可能不同)=
Linux:
Linux的:
/home/<user>
/android-ndk
/家庭/ <用户> /机器人-NDK
Mac OS X:
Mac OS X:
/Users/<user>
/android-ndk
/用户/ <用户> /机器人-NDK
In Terminal:
在终端:
# create tool-chain - one line
# New method in ndk 12.
$NDK/build/tools/make_standalone_toolchain.py --arch arm --install-dir=/tmp/my-android-toolchain
# Old method.
#$NDK/build/tools/make-standalone-toolchain.sh --platform=android-3 --install-dir=/tmp/my-android-toolchain
# add to terminal PATH variable
export PATH=/tmp/my-android-toolchain/bin:$PATH
# make alias CC be the new gcc binary
export CC=arm-linux-androideabi-gcc
# compile your C code(I tried hello world)
$CC -o foo.o -c foo.c
# push binary to phone
adb push foo.o /data/local/tmp
# execute binary
adb /data/local/tmp/foo.o
#3
3
Using CMake with the Android NDK is a nice way to compile Android console applications.
将CMake与Android NDK结合使用是编译Android控制台应用程序的好方法。
Download CMake and android-cmake (set it up like this). If your program is called main.c, then write the following in file CMakeLists.txt
:
下载CMake和android-cmake(像这样设置)。如果您的程序名为main.c,则在文件CMakeLists.txt中写入以下内容:
project(test)
cmake_minimum_required(VERSION 2.8)
add_executable(test ./main.c)
and run cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN .
并运行cmake -DCMAKE_TOOLCHAIN_FILE = $ ANDTOOLCHAIN。
You will then have a Makefile for your program, you can run make
to have your test
executable.
然后,您将拥有一个Makefile用于您的程序,您可以运行make来让您的测试可执行文件。
#4
0
Try if if the agcc wrapper can help you as referenced in the Android-tricks blog. According to the blog post you want to use the bionic library, but the one already installed on the phone, not some separately compiled version.
如果agcc包装器可以帮助您在Android-tricks博客中引用,请尝试。根据你想要使用仿生库的博客文章,但是已经安装在手机上的那个,而不是一些单独编译的版本。
#1
19
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
只需使用android-ndk。并像这样构建一个Android.mk。 include $(BUILD_EXECUTABLE)告诉它构建一个可执行文件而不是JNI .lib
Android.mk
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
#2
15
First, make sure you have the NDK:
首先,确保你有NDK:
http://developer.android.com/tools/sdk/ndk/index.html
http://developer.android.com/tools/sdk/ndk/index.html
Here is the easiest way to compile a C binary for your phone:
这是为手机编译C二进制文件的最简单方法:
http://developer.android.com/tools/sdk/ndk/index.html
http://developer.android.com/tools/sdk/ndk/index.html
http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html
http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html
Usually $NDK(may be different) =
通常$ NDK(可能不同)=
Linux:
Linux的:
/home/<user>
/android-ndk
/家庭/ <用户> /机器人-NDK
Mac OS X:
Mac OS X:
/Users/<user>
/android-ndk
/用户/ <用户> /机器人-NDK
In Terminal:
在终端:
# create tool-chain - one line
# New method in ndk 12.
$NDK/build/tools/make_standalone_toolchain.py --arch arm --install-dir=/tmp/my-android-toolchain
# Old method.
#$NDK/build/tools/make-standalone-toolchain.sh --platform=android-3 --install-dir=/tmp/my-android-toolchain
# add to terminal PATH variable
export PATH=/tmp/my-android-toolchain/bin:$PATH
# make alias CC be the new gcc binary
export CC=arm-linux-androideabi-gcc
# compile your C code(I tried hello world)
$CC -o foo.o -c foo.c
# push binary to phone
adb push foo.o /data/local/tmp
# execute binary
adb /data/local/tmp/foo.o
#3
3
Using CMake with the Android NDK is a nice way to compile Android console applications.
将CMake与Android NDK结合使用是编译Android控制台应用程序的好方法。
Download CMake and android-cmake (set it up like this). If your program is called main.c, then write the following in file CMakeLists.txt
:
下载CMake和android-cmake(像这样设置)。如果您的程序名为main.c,则在文件CMakeLists.txt中写入以下内容:
project(test)
cmake_minimum_required(VERSION 2.8)
add_executable(test ./main.c)
and run cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN .
并运行cmake -DCMAKE_TOOLCHAIN_FILE = $ ANDTOOLCHAIN。
You will then have a Makefile for your program, you can run make
to have your test
executable.
然后,您将拥有一个Makefile用于您的程序,您可以运行make来让您的测试可执行文件。
#4
0
Try if if the agcc wrapper can help you as referenced in the Android-tricks blog. According to the blog post you want to use the bionic library, but the one already installed on the phone, not some separately compiled version.
如果agcc包装器可以帮助您在Android-tricks博客中引用,请尝试。根据你想要使用仿生库的博客文章,但是已经安装在手机上的那个,而不是一些单独编译的版本。