Compile A native C Android application

Source: Internet
Author: User

Original: Compile A native C Android application
Translation: Zhiwei.li

By searching online, you can find many ways to compile Android native apps. What I would like to say is that different console applications, daemons (daemon), C + + libraries, and so on. These programs are on your own computer.
Compile without any problems.

In order to compile the program for Android, you need the ARM tool chain (toolchain). I have found two propositions, namely the use of Android Prebuild Toolchain and Codesourcery
1) Android Prebuild toolchain
Android does not use the traditional libc library. Instead, it uses the Bionic Library, a lightweight libc developed by Google and used on Android mobile software platforms.
The bionic is cropped to support only Android systems. See the $6 million C library

2) Codesourcery
Codesourcery is an arm partner. Developed the enhanced gun toolchain specifically for ARM processors, and provided a validated GNU Toolchain. There are many different versions of these tool chains.
For Android platforms, Arm-none-linux-gnueabi is required, and Arm-none-eabi is not glibc included, primarily for those compiling complete native libraries and applications (such as FreeRTOS)
Translator Note: Arm-none-eabi is used to compile bare metal programs, please refer to the simplest arm bare metal program

My personal point of view is that if you start from scratch to create an Android app, you should choose Bionic.
But if you choose to transplant a library from your PC environment to Android, you should choose Codesourcery
If you use a thread or C + + exception, the Bionic Library does not fully support them (in fact, it does not support exceptions at all)

Agcc.pl is a script developed by Andrew Ross that allows you to automatically include commonly used libraries in an easy way, using the Android arm toolchain GCC
In a way, he's like makefile.

The 1th method uses the Makefile and Android NDK

AR = Arm-linux-androideabi-aras = ARM-LINUX-ANDROIDEABI-ASCC = Arm-linux-androideabi-gcccxx = arm-linux-androideabi-g+ +ld = Arm-linux-androideabi-ld.goldndk_kit =/home/tim/android-ndk-r10bplatf_kit = Platforms/android-9arm_inc = $ (NDK_ KIT)/$ (platf_kit)/arch-arm/usr/includearm_lib = $ (ndk_kit)/$ (platf_kit)/arch-arm/usr/libobjs = Hello.oEXES = Hellohello:  hello.o$ (LD)--dynamic-linker=/system/bin/linker-nostdlib-rpath-link=$ (ARM_LIB) $ (ARM_LIB)/  crtbegin_dynamic.o-l$ (arm_lib)  -lc-o Hello hello.ohello.o:hello.c$ (CC)-I $ (arm_inc)-C hello.cclean:rm-f $ (OBJS) $ (EXES)

Source

#include <stdio.h>int main (int argc, char* argv[]) {  printf ("Hello android\n");  return 0;}

Set environment variable envsetup.sh

Export path= $PATH:/home/tim/android-ndk-r10b/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin

Finally, run make.

For-rpath-link options, refer to GCC link options

The second simple method, using shell scripts

#!/bin/shos= ' Linux ' androidsdk= ' android-14 ' progdir= '/home/tim/android-ndk-r10b/' progdir= ' CD $PROGDIR && PWD ' armeabigcc= $PROGDIR/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/ arm-linux-androideabi-gccarmeabilib= $PROGDIR/platforms/$ANDROIDSDK/arch-arm/usr/libarmeabiinc= $PROGDIR/ platforms/$ANDROIDSDK/arch-arm/usr/includearmeabicrt= "$ARMEABILIB/crtbegin_dynamic.o $ARMEABILIB/crtend_ ANDROID.O "Linker=/system/bin/linkerecho" GCC: "$ARMEABIGCC" LIB: "$ARMEABILIB" LINKER ": $LINKER" PARAMS: "[Email Protected]echo "CRT:" $ARMEABICRT $armeabigcc [email protected]-wl,-rpath-link= $ARMEABILIB,-dynamic-linker= $LINKER- L$armeabilib $ARMEABICRT-I$ARMEABIINC-NOSTDLIB-LC

Save As B
./b Hello.c-o Hello
You can do it.

is actually running the command

/home/tim/android-ndk-r10b/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/ ARM-LINUX-ANDROIDEABI-GCC   -wl,-rpath-link=/home/tim/android-ndk-r10b/platforms/android-16/arch-arm/usr/lib ,-dynamic-linker=/system/bin/linker   -l/home/tim/android-ndk-r10b/platforms/android-16/arch-arm/usr/lib    /HOME/TIM/ANDROID-NDK-R10B/PLATFORMS/ANDROID-16/ARCH-ARM/USR/LIB/CRTBEGIN_DYNAMIC.O   /home/tim/ ANDROID-NDK-R10B/PLATFORMS/ANDROID-16/ARCH-ARM/USR/LIB/CRTEND_ANDROID.O     -i/home/tim/android-ndk-r10b/ PLATFORMS/ANDROID-16/ARCH-ARM/USR/INCLUDE-NOSTDLIB-LC    Hello.c-o Hello

CRTBEGIN_DYNAMIC.O and CRTEND_ANDROID.O must be paired with

The third method, with –sysroot is also possible

#!/bin/shndk=/home/tim/android-ndk-r8esysroot= $NDK/platforms/android-9/arch-armcc= "$NDK/toolchains/ ARM-LINUX-ANDROIDEABI-4.4.3/PREBUILT/LINUX-X86_64/BIN/ARM-LINUX-ANDROIDEABI-GCC--sysroot= $SYSROOT "CFLAGS="- March=armv7-a-mfloat-abi=softfp-mfpu=neon ' ldflags= '-wl,--fix-cortex-a8 ' $CC [email protected]

http://www.srombauts.fr/2011/03/06/standalone-toolchain/

#include  <stdio.h> #include <android/log.h> #define LOGI (...) (void) __android_log_print (android_log_info, "Hello-ndk", __va_args__)) int main (void) {    printf ("Hello from ndkn" );    Logi ("Hello from NDK");    return 0;}

The above code can be compiled like this

./b9-l Log Nl.c-o HN

You can do it with makefile.

CC  = Arm-linux-androideabi-gcccflags  =-wall-gldflags =-llogsrc =hello-ndk.cobj =$ (src:.c=.o) EXE = Hello-ndkall: $ (SRC) $ (EXE) $ ((EXE) $ (OBJ)    $ (CC)-o [email protected] $^ $ (ldflags)%.O:%.c    $ (CC)-O [Email protect Ed]-C $< $ (CFLAGS) Clean:    rm-f *.o $ (EXE)

Compiling so libraries is also possible,

CC  = Arm-linux-androideabi-gcccflags  =-wall-gldflags =-llog-sharedsrc =hello-ndk.cobj =$ (SRC:.c=.o) EXE = Libhello-ndk.so

You can also run GCC directly

/home/tim/android-ndk-r10/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/ ARM-LINUX-ANDROIDEABI-GCC--sysroot=/home/tim/android-ndk-r10/platforms/android-3/arch-arm   -lc-lm-   g Main.c-o mm

The 4th method, with Ndk-build
Create a project catalog Hello, and then create a subdirectory under it JNI
Then create two files under Jni, one is hello.c, the other is android.mk, the content is as follows

Local_path:= $ (call My-dir) include $ (clear_vars) Local_src_files:=hello.clocal_module: = Helloworldlocal_module_tags : = Optionalinclude $ (build_executable)

where local_module_tags: = Optional This line can not

Go to the Hello directory and run the following command

# export ndk_project_path= ' pwd ' # ndk-buildcompile thumb  : HelloWorld <= hello.c executable     : HelloWorld Install        : HelloWorld = Libs/armeabi/helloworld

Compile A native C Android application

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.