Use the independent toolchain in Android ndk to develop C/C ++ programs

Source: Internet
Author: User

Some arm toolchains can be found online, but the Android system uses bionic libc instead of glibc. Therefore, only static compilation programs can be used.

In fact, Android ndk comes with toolchain, but you cannot directly use the toolchain in the ndk directory. Otherwise, the crtbegin_dynamic.o file cannot be found.

That is, you can use-L to specify the directory or directly put it in the GCC command line, and still prompt that the file cannot be found. (Refer to the link attached at the end ).

In fact, Android ndk provides a script to strip out a separate toolchain, The Script Name Is make-standalone-toolchain.sh

1. Download Android ndk

Http://developer.android.com/sdk/ndk/index.html
I use a android-ndk-r6b.

2. Extract toolchain

See docs/STANDALONE-TOOLCHAIN.html for Reference

Decompress the ndk in Linux, and then extract it to/OPT;

Android-ndk-r6b/CD/opt/
Build/tools/make-standalone-toolchain.sh -- platform = Android-8

Expr: Warning: unportable bre: '^ \ ([^ \-]. * \) $': Using '^' as the first character
Of the basic regular expression is not portable; it is being ignored
Expr: Warning: unportable bre: '^ \ (-- [^ =] * \) =. * $': Using '^' as the first character
Of the basic regular expression is not portable; it is being ignored
Expr: Warning: unportable bre: '^ -- [^ =] * =\\ (. * \) $': Using '^' as the first character
Of the basic regular expression is not portable; it is being ignored
Auto-config: -- toolchain = arm-linux-androideabi-4.4.3
Copying prebuilt binaries...
Copying sysroot headers and libraries...
Copying libstdc ++ headers and libraries...
Expr: Warning: unportable bre: '^ \ ([^ \-]. * \) $': Using '^' as the first character
Of the basic regular expression is not portable; it is being ignored
Expr: Warning: unportable bre: '^ \ (-- [^ =] * \) =. * $': Using '^' as the first character
Of the basic regular expression is not portable; it is being ignored
Expr: Warning: unportable bre: '^ \ (--. * \) $': Using '^' as the first character
Of the basic regular expression is not portable; it is being ignored
Expr: Warning: unportable bre: '^ \ ([^ \-]. * \) $': Using '^' as the first character
Of the basic regular expression is not portable; it is being ignored
Expr: Warning: unportable bre: '^ \ ([^ \-]. * \) $': Using '^' as the first character
Of the basic regular expression is not portable; it is being ignored
Expr: Warning: unportable bre: '^ \ ([^ \-]. * \) $': Using '^' as the first character
Of the basic regular expression is not portable; it is being ignored
Expr: Warning: unportable bre: '^ \ (-- [^ =] * \) =. * $': Using '^' as the first character
Of the basic regular expression is not portable; it is being ignored
Expr: Warning: unportable bre: '^ -- [^ =] * =\\ (. * \) $': Using '^' as the first character
Of the basic regular expression is not portable; it is being ignored
Creating package file:/tmp/ndk-Hansel/arm-linux-androideabi-4.4.3.tar.bz2
Cleaning up...
Done.

There are some warnings that don't matter and eventually get a compressed package/tmp/ndk-Hansel/arm-linux-androideabi-4.4.3.tar.bz2

Note: This is the directory on my Linux machine.

3. decompress the separate toolchain

You can decompress the package to any directory. Assume It is/opt/

4. Try writing a hello World Program.

Hello. c

# Include <stdlib. h>
# Include <stdio. h>

Int main (INT argc, char * argv [])
{
Printf ("Hello andriod. \ n ");
Return 0;
}
Makefile
Export path: =/opt/Android/arm-linux-androideabi-4.4.3/bin: $ {path}
Cross_compile = arm-Linux-androideabi-
Cc = $ (cross_compile) GCC
LD = $ (cross_compile) LD

Prog = Hello
Objs = Hello. o

$ (Prog): $ (objs)
$ (CC) $ (ldflags)-o $ @ $ (objs)

%. O: %. c
$ (CC)-C $ (cflags) $ <-o $ @

Clean:
Rm-RF *. o $ (Prog)
Compile:

Make

You can get the hello executable file.

$ File hello
Hello: Elf 32-bit LSB executable, arm, Version 1 (sysv), dynamically linked (uses SHARED libs), not stripped

It can be seen that the link is dynamic.

Upload the file to your mobile phone. If you have connected your mobile phone with a data cable and installed the android SDK, you can use the ADB command.

ADB push Hello/system/sbin/Hello

ADB shell chmod 777/system/sbin/Hello
ADB shell/system/sbin/Hello

If you do not have an SDK, you can install a quicksshd program on your mobile phone and connect to your mobile terminal using putty or other software such as WiFi. Transfer files through SFTP.

#./Hello
Hello andriod.

Note: The mobile phone requires the root permission.

Reference: http://groups.google.com/group/android-ndk/browse_thread/thread/7506768ccf52cea2? PLI = 1

Http://www.linuxidc.com/Linux/2011-10/45968.htm

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.