Android compiling environment (2)-manually compile the C Module

Source: Internet
Author: User

Http://www.top-e.org/jiaoshi/html? 161. html

 

In the previous article 《Android compiling environment (1)-compile the helloworld module of native C), We tried to pass the standardAndroid. mkFile to compile the C Module. Next we will try to use the GCC command line to compile it, so as to understand the details of the android compiling environment.

The android compilation environment provides the "showcommands" option to display the compilation command line. We can open this option to view some details during compilation. Of course, before that, we should clean the helloworld module in the previous article:

# Make clean-helloworld

The above "make clean-$ (local_module)" is the make clean method provided by the android compiling environment.

 

Next, use the showcommands option to re-compile helloworld:

# Make helloworld showcommands

Build/CORE/product_config.mk: 229: Warning: adding test OTA key

Target thumb C: helloworld <= Development/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-dctor arm_arch_5 _-d1_arm_arch_5t _-d1_arm_arch_5e _-d1_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 /product/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

 

From the command line above, we can see that the cross-compilation tool chain used by the android compiling environment is prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/ARM-Eabi-GCC, the-I and-l Parameters specify the C-library header files and the dynamic library file paths respectively: bionic/libc/include and out/target/product/generic/obj/lib, others also include many compilation options and pre-compiled macros defined by-D.

 

We can use the above compilation command to make it easier to manually compile the helloworld program. Manually delete the last compiled helloworld program:

# Rm out/target/product/generic/obj/executables/helloworld_intermediates/Hello. o

# Rm out/target/product/generic/system/bin/helloworld

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/ kernel/common-I bionic/libc/kernel/arch-arm-C-fno-tions-wno-multichar-March = armv5te-mtune = XScale-msoft-float-FPIC-mthumb -interwork-ffunction-sections-funwind-tables-fstack-protector-d1_arm_arch_5 _-d1_arm_arch_5t _-character _-d1_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 with Android. mk compilation parameters, the above mainly reduces unnecessary-I parameters.

Next, generate the executable file:

# Pre built/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" specifies the Android-specific dynamic linker/system/bin/linker, instead of the commonly used lD. so.

 

The generated executable program can be viewed using the file and readelf commands:

# File out/target/product/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 a dynamic link executable file in arm format. libc. So and libm. So are required for running. "Not stripped" indicates that it has not been strip. In an embedded system, the compiled executable files or dynamic libraries are strip to save space. That is, redundant symbol table information is removed. At the end of the "make helloworld showcommands" command, we can also see that the out/host/linux-x86/bin/soslim tool is used in the android compiling environment for strip.

For more information about Android toolchain, see:Android toolchain and bionic libc

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.