The "Go" Android C program can also be compiled by hand using the cross compiler (

Source: Internet
Author: User

Original URL: http://blog.sina.com.cn/s/blog_533074eb0101ez5q.html

The Android build environment itself is complex, and unlike a normal compilation environment: only the top-level directories have Makefile files, while each of the other component uses a uniform standard of android.mk . android.mk file itself is relatively simple, but it is not familiar with the makefile, but after the Android itself compiled system of many processing, so to really understand the connection is more complex, but the advantage of this way is that Writing a new android.mk to add a new Component to Android will be easier.

Compiling a Java program can be done directly using Eclipse's integrated environment, which is not duplicated here. We are mainly for the C + + to illustrate, below a small example to illustrate how to add a C program in Android Hello World:

1. Create a Hello directory under the $ (your_android)/development directory, where $ (your_android) refers to the directory where the ANDROID source code resides.
-# mkdir $ (your_android)/development/hello

2. In the $ (your_android)/development/hello/directory to write hello.c files, HELLO.C's content is of course the classic HelloWorld program:

#include

int main ()
{
printf ("Hello world!/n");

return 0;
}

3. Write the Android.mk file in the $ (your_android)/development/hello/directory. This is the standard name for Android Makefile, do not change. The format and content of the Android.mk file can be referenced in other existing android.mk files, and the contents of the Android.mk file for the HelloWorld program are as follows:

Local_path:= $ (call My-dir)

Include $ (clear_vars)

local_src_files:=/

hello.c

Local_module: = HelloWorld

Include $ (build_executable)

Note that the above local_src_files is used to specify the source file, Local_module specifies the name of the module to be compiled, and the next step is to compile it; include $ (build_executable) means to compile into an executable file, If you want to compile into a dynamic library, you can use Build_shared_library, which is found in the $ (your_android)/build/core/config.mk.

4. Go back to the top-level directory for Android source code to compile:

# CD $ (your_android) && make HelloWorld

Note that the target name HelloWorld in Make HelloWorld is the module name specified by Local_module in the Android.mk file above. The compilation results are as follows:

Target Thumb C:helloworld <= development/hello/hello.c

Target Executable:helloworld (out/target/product/generic/obj/executables/helloworld_intermediates/linked/ HelloWorld

Target Non-prelinked:helloworld (Out/target/product/generic/symbols/system/bin/helloworld)

Target Strip:helloworld (Out/target/product/generic/obj/executables/helloworld_intermediates/helloworld)

Install:out/target/product/generic/system/bin/helloworld

5. As shown in the previous compilation results, the compiled executable is stored in the Out/target/product/generic/system/bin/helloworld, passed to the emulator via "ADB push", and then through the "adb shell" Log in to the emulator terminal and you can execute the

============================================

In the previous article ("Android compilation Environment (1)-Compile Native C HelloWorld module"), we tried to compile the C module through the standard android.mk file, let's try to use the GCC command line to compile directly, Learn the details of the Android build environment.

The Android compilation environment provides the "showcommands" option to display the compile command line, and we can view some compile-time details by opening this option. Of course, before you do this, clean the HelloWorld module in the previous article:

# Make Clean-helloworld

The "Make clean-$" above is the way the Android build environment provides for make clean.

Next, recompile the HelloWorld using the showcommands option:

# Make HelloWorld showcommands

Build/core/product_config.mk:229:warning:adding Test OTA Key

Target Thumb C:helloworld <= development/hello/hello.c

Prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc  -i system/core/include    -i hardware/libhardware/include   -i hardware/ril/include   -i dalvik/ Libnativehelper/include   -i frameworks/base/include   -i External/skia/include    -i out/target/product/generic/obj/include   -i Bionic/libc/arch-arm/include    -i bionic/libc/include   -i bionic/libstdc++/include   -i bionic/ Libc/kernel/common   -i bionic/libc/kernel/arch-arm   -i bionic/libm/include    -i bionic/libm/include/arch/arm   -i Bionic/libthread_db/include   -i Development/hello   -i out/target/product/generic/obj/executables/helloworld_intermediates   -c  -fno-exceptions-wno-multichar-march=armv5te-mtune=xscale-msoft-float-fpic-mthumb-interwork -ffunction-sections-funwind-tables-fstack-protector-d__arm_arch_5__-d__arm_arch_5t__-D__ARM_ARCH_5E__-D__ARM_ARCH_ 5te__-include system/core/include/arch/linux-arm/androidconfig.h-dandroid-fmessage-length=0-w-wall-wno-unused- Dsk_release-dndebug-o2-g-wstrict-aliasing=2-finline-functions-fno-inline-functions-called-once- Fgcse-after-reload-frerun-cse-after-loop-frename-registers-dndebug-udebug-mthumb-os-fomit-frame-pointer- Fno-strict-aliasing-finline-limit=64      -md-o out/target/product/generic/obj/ EXECUTABLES/HELLOWORLD_INTERMEDIATES/HELLO.O DEVELOPMENT/HELLO/HELLO.C

Target Executable:helloworld (out/target/product/generic/obj/executables/helloworld_intermediates/linked/ HelloWorld

prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-g++-nostdlib-bdynamic-wl,-t,build/core/armelf.x-wl,- DYNAMIC-LINKER,/SYSTEM/BIN/LINKER-WL,--Gc-sections-wl,-z,nocopyreloc-o out/target/product/generic/obj/ executables/helloworld_intermediates/linked/helloworld-lout/target/product/generic/obj/lib-wl,-rpath-link=out/ target/product/generic/obj/lib-lc-lstdc++-LM OUT/TARGET/PRODUCT/GENERIC/OBJ/LIB/CRTBEGIN_DYNAMIC.O out/target/p RODUCT/GENERIC/OBJ/EXECUTABLES/HELLOWORLD_INTERMEDIATES/HELLO.O-WL,--no-undefined prebuilt/linux-x86/toolchain/ arm-eabi-4.2.1/bin/. /LIB/GCC/ARM-EABI/4.2.1/INTERWORK/LIBGCC.A OUT/TARGET/PRODUCT/GENERIC/OBJ/LIB/CRTEND_ANDROID.O

Target Non-prelinked:helloworld (Out/target/product/generic/symbols/system/bin/helloworld)

OUT/HOST/LINUX-X86/BIN/ACP-FPT out/target/product/generic/obj/executables/helloworld_intermediates/linked/ HelloWorld Out/target/product/generic/symbols/system/bin/helloworld

Target Strip:helloworld (Out/target/product/generic/obj/executables/helloworld_intermediates/helloworld)

Out/host/linux-x86/bin/soslim--strip--shady--quiet Out/target/product/generic/symbols/system/bin/helloworld-- OutFile Out/target/product/generic/obj/executables/helloworld_intermediates/helloworld

Install:out/target/product/generic/system/bin/helloworld

OUT/HOST/LINUX-X86/BIN/ACP-FPT Out/target/product/generic/obj/executables/helloworld_intermediates/helloworld Out/target/product/generic/system/bin/helloworld

As you can see from the command line above, the cross-compilation toolchain used by the Android compilation environment is PREBUILT/LINUX-X86/TOOLCHAIN/ARM-EABI-4.2.1/BIN/ARM-EABI-GCC, and the-I and-l parameters specify the C The Library header files and the dynamic library file paths are bionic/libc/include and Out/target/product/generic/obj/lib, and others include many compilation options as well as the pre-compiled macros defined by-D.

We can use the above compiler command, a little simplification to manually compile the HelloWorld program. Delete the last compiled HelloWorld program by hand:

# RM OUT/TARGET/PRODUCT/GENERIC/OBJ/EXECUTABLES/HELLOWORLD_INTERMEDIATES/HELLO.O

# RM Out/target/product/generic/system/bin/helloworld

Then compile with GCC to generate the target file:

# prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc-i BIONIC/LIBC /arch-arm/include-i bionic/libc/include-i bionic/libc/kernel/common   -i bionic/libc/kernel/ Arch-arm-c  -fno-exceptions-wno-multichar-march=armv5te-mtune=xscale-msoft-float-fpic- mthumb-interwork-ffunction-sections-funwind-tables-fstack-protector-d__arm_arch_5__-D__ARM_ARCH_5T__-D__ARM_ arch_5e__-d__arm_arch_5te__-include System/core/include/arch/linux-arm/androidconfig.h-dandroid-fmessage-length =0-w-wall-wno-unused-dsk_release-dndebug-o2-g-wstrict-aliasing=2-finline-functions- Fno-inline-functions-called-once-fgcse-after-reload-frerun-cse-after-loop-frename-registers-dndebug-udebug- Mthumb-os-fomit-frame-pointer-fno-strict-aliasing-finline-limit=64      -md-o out /TARGET/PRODUCT/GENERIC/OBJ/EXECUTABLES/HELLOWORLD_INTERMEDIATES/HELLO.O development/hello/hello.c

Compared to the ANDROID.MK compilation parameters, it mainly reduces the unnecessary-I parameters.

Next build the executable file:

# prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc-nostdlib-bdynamic-wl,-t,build/core/armelf.x-wl,- DYNAMIC-LINKER,/SYSTEM/BIN/LINKER-WL,--Gc-sections-wl,-z,nocopyreloc-o out/target/product/generic/obj/ executables/helloworld_intermediates/linked/helloworld-lout/target/product/generic/obj/lib-wl,-rpath-link=out/ TARGET/PRODUCT/GENERIC/OBJ/LIB-LC-LM out/target/product/generic/obj/executables/helloworld_intermediates/ HELLO.O out/target/product/generic/obj/lib/crtbegin_dynamic.o-wl,--no-undefined./prebuilt/linux-x86/toolchain/ arm-eabi-4.2.1/bin/. /LIB/GCC/ARM-EABI/4.2.1/INTERWORK/LIBGCC.A OUT/TARGET/PRODUCT/GENERIC/OBJ/LIB/CRTEND_ANDROID.O

It is worth noting that the parameter "-wl,-dynamic-linker,/system/bin/linker", which specifies the Android dedicated dynamic linker/system/bin/linker, rather than the usual ld.so.

The resulting executable program can be viewed using the file and Readelf commands:

# file out/target/p Roduct/generic/obj/executables/helloworld_intermediates/linked/helloworld

Out/target/product/generic/obj /executables/helloworld_intermediates/linked/helloworld:elf 32-bit LSB executable, ARM, version 1 (SYSV), dynamically Linked (uses shared libs), not stripped

#  readelf-d out/target/product/generic/obj/executables/ Helloworld_intermediates/linked/helloworld |grep NEEDED

 0x00000001 (NEEDED)                       shared Library: [libc.so]

 0x00000001 (NEEDED)                       shared library: [libm.so]

This is the dynamic link executable file in ARM format, which requires libc.so and libm.so at runtime. "Not stripped" means it hasn't been STRIP yet. In an embedded system, to save space, the compiled executable or dynamic library is usually STRIP, which removes the redundant symbol table information. At the end of the "Make HelloWorld showcommands" command, we can also see that the Android compilation environment uses the Out/host/linux-x86/bin/soslim tool for strip.

======================================

The toolchain (i.e. cross-compilation toolchain) used by Android can be downloaded from the following URLs:

http://android.kernel.org/pub/android-toolchain-20081019.tar.bz2. If you downloaded the full Android project's source code, you can find the cross-compilation tool in the "/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin" directory, such as Android arm-eabi-gcc-4.2.1.

Android does not use glibc as a C library, but instead uses Google's own Bionic LIBC, whose official toolchain is based on Bionic Libc and not glibc. This makes it more troublesome to use or transplant other toolchain for Android: Most Android enthusiasts use Toolchain before Google announces the official toolchain for Android Http://www.cod esourcery.com/gnu_toolchains/arm/download.html download a generic toolchain, which is useful for compiling and porting Android Linux Kernels, because the kernel does not need C libraries, but development Android applications, the direct adoption or porting of other toolchain are cumbersome, and other toolchain compiled applications can only be statically compiled in order to run in the Android emulator, which is clearly unacceptable in the actual development of the way. There is no information yet to illustrate the successful porting of other cross compilers to compile Android apps.

Compared with glibc, Bionic LIBC has the following features:

-Use BSD License, not glibc's GPL License;

-Only about 200k in size, half as small as glibc, and faster than glibc;

-Achieve a smaller, faster pthread;

-provides some important functions required by Android, such as "Getprop", "Logi" and so on;

-Does not fully support POSIX standards, such as C + + exceptions, wide chars, etc.;

-No implementations of libthread_db and LIBM are provided

In addition, some of the other binary tools used in Android are also very special:

-use/system/bin/linker instead of common/lib/ld.so when loading dynamic libraries;

-PreLink tool is not a common prelink but Apriori, its source code is located in "/build/tools/apriori"

-The Strip tool does not use the usual strip, the Arm-eabi-strip in the "/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin" directory, but is located in/ out/host/linux-x86/bin/的soslim 工具。

The "Go" Android C program can also be compiled by hand using the cross compiler (

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.