Four compilation methods of Helloworld in Android Native C

Source: Internet
Author: User
Document directory
  • 1. Compile helloworld. c Android. mk
1. Compile helloworld. c Android. mk

[Root @ fontlose jni] # cat hello. c

[Cpp]
View plaincopyprint?
  1. # Include <stdio. h>
  2. Int main ()
  3. {
  4. Printf ("Hello World! \ N ");
  5. Return 0;
  6. }

[Root @ fontlose jni] # cat Android. mk

[Plain]
View plaincopyprint?
  1. LOCAL_PATH: = $ (call my-dir)
  2. Include $ (CLEAR_VARS)
  3. LOCAL_SRC_FILES: = hello. c
  4. LOCAL_MODULE: = helloworld
  5. LOCAL_MODULE_TAGS: = optional
  6. Include $ (BUILD_EXECUTABLE)
Ii. Compile 1) Use NDK r5

Directory structure-hello
── Jni
── Android. mk

── Hello. c

[Plain]
View plaincopyprint?
  1. [Root @ fontlose nativity] # cd hello
  2. [Root @ fontlose hello] # export NDK_PROJECT_PATH = 'pwd'
  3. [Root @ fontlose hello] # ndk-build
  4. Install: helloworld => libs/armeabi/helloworld
  5. [Root @ fontlose hello] # adb push libs/armeabi/helloworld/data
  6. 44 KB/s (2176 bytes in 0.047 s)
  7. [Root @ fontlose hello] # adb shell
  8. /# Cd/data
  9. /Data # ls-l
  10. -Rwxrwxrwx root 2176 2011-08-07 0:01 helloworld
  11. /Data #./helloworld
  12. Hello World!
2) use the general toolchain

Under the hello. c directory[Plain]
View plaincopyprint?

  1. [Root @ fontlose jni] # arm-none-linux-gnueabi-gcc-static hello. c-o helloworld
  2. [Root @ fontlose jni] # adb push helloworld/data
  3. 1718 KB/s (581219 bytes in 0.330 s)
  4. [Root @ fontlose jni] # adb shell
  5. /# Cd/data
  6. /Data # ls-l
  7. -Rwxrwxrwx Root 581219 helloworld
  8. /Data #./helloworld
  9. Hello world!

Windows:

Http://www.codesourcery.com/gnu_toolchains/arm/portal/package3400/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.exe

Linux:
Http://www.codesourcery.com/gnu_toolchains/arm/portal/package3399/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.bin

3) compile in the source code Environment

1. Create the hello directory under the development Directory of the source code.[Plain]
View plaincopyprint?

  1. [Root @ fontlose Android-2.3] # mkdir $ android_src/development/Hello

2. Copy helloworld. c Android. mk to the hello directory.
3. [root @ fontlose Android-2.3] #. build/envsetup. sh

4. [root @ fontlose Android-2.3] # mmm development/hello/

[Plain]
View plaincopyprint?
  1. ========================================================== ====
  2. Platform_version_codename = REL
  3. Platform_version = 2.3.2
  4. Target_product = generic
  5. Target_build_variant = ENG
  6. Target_simulator =
  7. Target_build_type = release
  8. TARGET_BUILD_APPS =
  9. TARGET_ARCH = arm
  10. HOST_ARCH = x86
  11. HOST_ OS = linux
  12. HOST_BUILD_TYPE = release
  13. BUILD_ID = GRH78C
  14. ========================================================== ====
  15. Make: Enter the directory "/opt/FriendlyARM/mini6410/android/Android-2.3"
  16. Target thumb C: helloworld <= development/hello. c
  17. Target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)
  18. Target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)
  19. Target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)
  20. Install: out/target/product/generic/system/bin/helloworld
  21. Make: Leave the directory "/opt/FriendlyARM/mini6410/android/Android-2.3"

5. After compilation is completed, the execution file is in out/target/product/generic/symbols/system/bin/helloworld.
Download To device[Plain]
View plaincopyprint?

  1. [Root @ fontlose Android-2.3] # adb push out/target/product/generic/symbols/system/bin/helloworld/data
  2. 135 KB/s (7608 bytes in 0.054 s)
  3. [Root @ fontlose Android-2.3] # adb shell
  4. /# Cd/data
  5. /Data # ls-l
  6. -Rwxrwxrwx Root 7608 helloworld
  7. /Data #./helloworld
  8. Hello world!
4) use the Toolchain provided by the source code

Before that, we need to clean the helloworld module in the previous step:[Plain]
View plaincopyprint?

  1. [Root @ fontlose Android-2.3] # Make clean-helloworld

Use the showcommands option to re-compile helloworld and view the specific commands. The output command line shows that, the cross-compilation tool chain used by the Android compiling environment is prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gcc.[Plain]
View plaincopyprint?

  1. [Root @ fontlose Android-2.3] # Make helloworld showcommands

Use the compilation command output above to simplify the manual compilation of the helloworld program. First, manually delete the helloworld program compiled last time:[Plain]
View plaincopyprint?

  1. [Root @ fontlose Android-2.3] # rm out/target/product/generic/system/bin/helloworld

Gcc compilation to generate the target file[Plain]
View plaincopyprint?

  1. [Root @ fontlose Android-2.3] # prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gcc-I development/hello-I out/target/product/generic/obj /EXECUTABLES/helloworld_intermediates-I dalvik/libnativehelper/include/nativehelper-I system/core/include-I hardware/libhardware/include-I hardware/ril/ include-I dalvik/libnativehelper/include-I frameworks/base/opengl/include-I frameworks/base/native/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/arch/arm-I bionic/libthread_db/include-c-fno-exceptions-Wno-multichar-msoft-float-fpic-ffunction-sections-funwind-tables-fstack-protector-Wa, -- noexecstack-Werror = format-security-fno-short-enums-march = armv5te-mtune = xscale-d1_arm_arch_5 _-region _-d1_arm_arch_5te _-include system/core/include/arch/linux-arm/AndroidConfig. h-I system/core/include/arch/linux-arm/-Wno-psabi-mthumb-interwork-DANDROID-fmessage-length = 0-W-Wall-Wno-unused- winit-self-Wpointer-arith-Werror = return-type-Werror = non-virtual-dtor-Werror = address-Werror = sequence-point-DNDEBUG-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. c

Generate an executable file:[Plain]
View plaincopyprint?

  1. [Root @ fontlose Android-2.3] # prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/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,-Z, noexecstack-wl, -- no-undefined prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin /.. /lib/GCC/ARM-Eabi/4.4.3/libgcc. A out/target/product/generic/obj/lib/crtend_android.o

STRIP using out/host/linux-x86/bin/soslim Tool[Plain]
View plaincopyprint?

  1. [Root @ fontlose Android-2.3] # 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
[Plain]
View plaincopyprint?
  1. [Root @ fontlose Android-2.3] # adb push out/target/product/generic/symbols/system/bin/helloworld/data
  2. 145 KB/s (7608 bytes in 0.051 s)
  3. [Root @ fontlose Android-2.3] # adb shell
  4. /# Cd/data/
  5. /Data # ls-l
  6. -Rwxrwxrwx root 7608 helloworld
  7. /Data #./helloworld
  8. Hello World!

Android does not use glibc as the C library, but uses the Bionic Libc developed by Google. it is feasible to use the general Toolchain to compile and transplant the Android Linux kernel, because the kernel does not need the C library, however, when developing Android applications, other Toolchain-compiled applications can only run in static compiling mode, and the execution files compiled in static mode are relatively large.

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.