NDK r19 to build standalone app/HelloWorld on Android

本文介绍从NDKr19开始的独立工具链使用方法,对比Clang与GCC工具链,并提供在Linux与Windows环境下使用NDK构建Android应用的具体步骤。包括不同Android版本下对位置独立代码的支持及构建过程中的符号表分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Standalone toolchain from NDK r19/2019
Google/https://developer.android.com/ndk/guides/other_build_systems

Using the NDK with other build systems
Note: The content described in this document requires at least NDK r19. If you're using an older NDK, consider upgrading. If you're unable to upgrade, see the Standalone Toolchain guide.
https://developer.android.com/ndk/guides/standalone_toolchain

Standalone Toolchains
Warning: If using r19 or newer, follow the Other Build Systems document for instructions on using the NDK toolchains with arbitrary build systems. As of r19, the NDK's default toolchains are standalone toolchains, which renders this process unnecessary.

2. Clang VS GCC toolchain from NDKn11/2016

Android NDK r11 recommends clang instead of GCC/deprecated/4.9

3. ############## Linux/AOSP7.1.2/NDK prebuilts/arm-linux-androideabi-4.9-GCC 
   ### this is platform build with AOSP 
lake@localhost:~/hello$ which  arm-linux-androideabi-gcc
/localrepo/lake/fmsng/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc
### Note  please source   build/envsetup.sh    and launch  xxxxxx

Build 
arm-linux-androideabi-gcc  hello.c   --sysroot=/localrepo/lake/fmsng/prebuilts/ndk/current/platforms/android-24/arch-arm     -o  hellondk  

lake@localhost:~/hello$ file hellondk 
hellondk: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /system/, not stripped
lake@localhost:~/hello$ 


3.1. Android version before Android LL 5.0 
lake@localhost:~/hello$ adb    push    hellondk  /data/local/tmp/
hellondk: 1 file pushed. 0.8 MB/s (6744 bytes in 0.008s)
lake@localhost:~/hello$ adb shell   chmod  a+x   /data/local/tmp/hellondk
lake@localhost:~/hello$ adb shell   /data/local/tmp/hellondk
Hello World


3.2. For Android version after Android LL5.0, addd CFLAG+= -fPIE -pie  

lake@localhost:~/hello$ adb shell   /data/local/tmp/hellondk
"/data/local/tmp/hellondk": error: only position independent executables (PIE) are supported.

lake@localhost:~/hello$ arm-linux-androideabi-gcc  hello.c   --sysroot=/localrepo/lake/fmsng/prebuilts/ndk/current/platforms/android-24/arch-arm     -o  hellondk   -fPIE -pie  

lake@localhost:~/hello$ adb push hellondk   /data/local/tmp/[100%] /data/local/tmp/hellondk
lake@localhost:~/hello$ adb shell   chmod  a+x   /data/local/tmp/hellondk
lake@localhost:~/hello$ adb shell   /data/local/tmp/hellondk
Hello World

lake@localhost:~/hello$ 

4. Window NDK build n19/2019 arm-linux-androideabi-4.9-clang (llvm + clang)
C:\work\Hello-Git\Hello\src>C:\Android\AndroidStudio\android-sdk-windows\ndk-bundle\toolchains\llvm\prebuilt\windows-x86
_64\bin\armv7a-linux-androideabi24-clang.cmd     Hello.c   -o hellondk

C:\work\Hello-Git\Hello\src>C:\Android\AndroidStudio\android-sdk-windows\ndk-bundle\toolchains\arm-linux-androideabi-4.9
\prebuilt\windows-x86_64\bin\arm-linux-androideabi-readelf.exe  -s   hellondk

Symbol table '.dynsym' contains 8 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 00000000     0 FUNC    GLOBAL DEFAULT  UND __cxa_atexit@LIBC (2)
     2: 00000000     0 FUNC    GLOBAL DEFAULT  UND __libc_init@LIBC (2)
     3: 00000000     0 FUNC    GLOBAL DEFAULT  UND __register_atfork@LIBC (2)
     4: 00000000     0 FUNC    GLOBAL DEFAULT  UND printf@LIBC (2)
     5: 0000202c     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
     6: 00002030     0 NOTYPE  GLOBAL DEFAULT  ABS _end
     7: 0000202c     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start

Symbol table '.symtab' contains 37 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtbegin.c
     2: 000003cc     0 NOTYPE  LOCAL  DEFAULT   11 $a.0
     3: 00000428     0 NOTYPE  LOCAL  DEFAULT   11 $a.2
     4: 00000454     0 NOTYPE  LOCAL  DEFAULT   11 $a.4
     5: 00000418     0 NOTYPE  LOCAL  DEFAULT   11 $d.1
     6: 0000044c     0 NOTYPE  LOCAL  DEFAULT   11 $d.3
     7: 00000460     0 NOTYPE  LOCAL  DEFAULT   11 $d.5
     8: 000003d4    84 FUNC    LOCAL  DEFAULT   11 _start_main
     9: 000001c0     0 NOTYPE  LOCAL  DEFAULT    2 ndk_build_number
    10: 00000180     0 NOTYPE  LOCAL  DEFAULT    2 ndk_version
    11: 00000168   152 OBJECT  LOCAL  DEFAULT    2 note_android_ident
    12: 0000017c     0 NOTYPE  LOCAL  DEFAULT    2 note_data
    13: 00000200     0 NOTYPE  LOCAL  DEFAULT    2 note_end
    14: 00000174     0 NOTYPE  LOCAL  DEFAULT    2 note_name
    15: 00000000     0 FILE    LOCAL  DEFAULT  ABS Hello.c
    16: 00000464     0 NOTYPE  LOCAL  DEFAULT   11 $a.0
    17: 00000498     0 NOTYPE  LOCAL  DEFAULT   11 $d.1
    18: 00000428    12 FUNC    LOCAL  HIDDEN    11 __atexit_handler_wrapper
    19: 0000202c     4 OBJECT  LOCAL  HIDDEN    19 __dso_handle
    20: 00000434    32 FUNC    LOCAL  HIDDEN    11 atexit
    21: 00000454    16 FUNC    LOCAL  HIDDEN    11 pthread_atfork
    22: 00001f08   248 OBJECT  LOCAL  HIDDEN    17 _DYNAMIC
    23: 00002010     0 OBJECT  LOCAL  HIDDEN    18 _GLOBAL_OFFSET_TABLE_
    24: 00000388     0 NOTYPE  LOCAL  DEFAULT   10 $a
    25: 00001f00     4 OBJECT  GLOBAL DEFAULT   16 __FINI_ARRAY__
    26: 00001ef8     4 OBJECT  GLOBAL DEFAULT   15 __INIT_ARRAY__
    27: 00001ef0     4 OBJECT  GLOBAL DEFAULT   14 __PREINIT_ARRAY__
    28: 00000000     0 FUNC    GLOBAL DEFAULT  UND __cxa_atexit
    29: 00000000     0 FUNC    GLOBAL DEFAULT  UND __libc_init
    30: 00000000     0 FUNC    GLOBAL DEFAULT  UND __register_atfork
    31: 000003cc     8 FUNC    GLOBAL DEFAULT   11 _start
    32: 00000464    56 FUNC    GLOBAL DEFAULT   11 main
    33: 00000000     0 FUNC    GLOBAL DEFAULT  UND printf
    34: 0000202c     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
    35: 00002030     0 NOTYPE  GLOBAL DEFAULT  ABS _end
    36: 0000202c     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start

C:\work\Hello-Git\Hello\src>
C:\work\Hello-Git\Hello\src>C:\Android\AndroidStudio\android-sdk-windows\ndk-bundle\toolchains\arm-linux-androideabi-4.9
\prebuilt\windows-x86_64\bin\arm-linux-androideabi-nm.exe     hellondk
00000428 t __atexit_handler_wrapper
0000202c A __bss_start
         U __cxa_atexit
0000202c b __dso_handle
00001f00 T __FINI_ARRAY__
00001ef8 T __INIT_ARRAY__
         U __libc_init
00001ef0 D __PREINIT_ARRAY__
         U __register_atfork
00001f08 d _DYNAMIC
0000202c A _edata
00002030 A _end
00002010 d _GLOBAL_OFFSET_TABLE_
000003cc T _start
000003d4 t _start_main
00000434 t atexit
00000464 T main
000001c0 r ndk_build_number
00000180 r ndk_version
00000168 r note_android_ident
0000017c r note_data
00000200 r note_end
00000174 r note_name
         U printf
00000454 t pthread_atfork

C:\work\Hello-Git\Hello\src> 
C:\work\Hello-Git\Hello\src>c:\Android\adb\adb.exe   push    hellondk  /data/local/tmp/
hellondk: 1 file pushed. 0.8 MB/s (6744 bytes in 0.008s)
C:\work\Hello-Git\Hello\src>adb shell   chmod  a+x   /data/local/tmp/hellondk
 

C:\work\Hello-Git\Hello\src>adb shell   chmod  a+x   /data/local/tmp/hellondk

C:\work\Hello-Git\Hello\src>adb shell     /data/local/tmp/hellondk
Hello world!

C:\work\Hello-Git\Hello\src>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值