Android Combat Tips 44: hello,native!

Source: Internet
Author: User

Running the C program on Android is a bit strange for a kid's shoe to do the top app, because the current Android app development is still not going to go around Java.
But for the low-level driver developers, this is the same routine, because Android is a Linux branch, the bottom is the C/s world.

Sometimes in order to test some features, we will also write a C program that runs directly under Android Terminal. The prerequisite is the Android cross compiler and the root authority of the Android system.

Cross-compilation tool

The NDK has done a lot of work for us to develop the native program, and we've separated the Android cross-compiler tool from the NDK.
My system is 64-bit Ubuntu 14.04, so I downloaded the 64-bit NDK (Android-ndk-r10e-linux-x86_64.bin).

ndk$ chmod a+x android-ndk-r10e-linux-x86_64../android-ndk-r10e-linux-x86_64.bin

At this point, the NDK is ready to work. Let's change the cross-compiler tool out.

$./build/tools/make-standalone-toolchain.Sh--Platform=Android- + --Toolchain=Arm-linux-androideabi-4.9Copying prebuilt Binaries...Copying sysroot Headers andLibraries...Copying C++Runtime headers andLibraries...Creating Package File:/TMP/NDK-linc/arm-linux-androideabi-4.9.Tar.Bz2cleaning up...Done.

Find the right path to unzip:

build-tools$ tar jxvf arm-linux-androideabi-4.9.tar.bz2arm-linux-androideabi-4.9/...
Hello,native

Compiling c files main_test.c

#include <stdio.h>int main() {    printf("just a test,linc!hello, native!\n");    return0;}

Compile it:

$ ~/bin/build-tools/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc-o main_test main_test.c

Run up:
To run Main_test to Android:

$  adb push main_test/data/ App137  kb /S (6192  bytes in  0 . 043  s) $  adb shellroot  @hammerhead  :/ # cd data/app  root @hammerhead  :/data/app  # ls  main_ Testroot @hammerhead  :/data/app  #./main_test  just a Test,linc!hello, native!  

As we wish, the program runs smoothly, just as it does in a Linux system. Let's compile a program of two files.
Shooter.c

#include "shooter.h"#include <stdio.h>voidBubble_sort (int*Array,intN) {inti,j,tmp; for(i=0; i<n-1; i++) { for(j=n-1; j>i;j--) {if(Array[J-1]>Array[j]) {TMP =Array[J-1];Array[J-1]=Array[j];Array[J]=tmp; }        }    }}intAintA) {intn =Ten;intIint Array[] = { Wu, A,346,5, at, the,234,324, $,98}; for(i=0; i<n;i++) {printf("%d,",Array[i]); }printf("\ n"); Bubble_sort (Array, n);return Array[0];}

Shooter_tester.c

#include <stdio.h>#include "shooter.h"int main() {    int result = A(0);    printf("A result: %d\n",result);    return0;}

Compile run:

$ ~/bin/build-tools/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc-o Test shooter_tester.c SHOOTER.C$ ADB push test/data/app/143 KB/s (6344bytesinch 0.043S$ ADB shellroot@hammerhead:/ # CD Data/appRoot@hammerhead:/data/app #./test Wu, A,346,5, at, the,234,324, $,98,A Result: 5
Using random numbers

Next try to test the code in the "Make a dynamic link library" under Android and prepare to port it to the Android platform.
SHOOTER.C just generates random numbers using the above program with Rand and Srand.

#include "shooter.h"#include <time.h>#include <stdio.h>voidBubble_sort (int*Array,intN) {inti,j,tmp; for(i=0; i<n-1; i++) { for(j=n-1; j>i;j--) {if(Array[J-1]>Array[j]) {TMP =Array[J-1];Array[J-1]=Array[j];Array[J]=tmp; }        }    }}intAintA) {intn =Ten;intIint Array[n]; Srand (Time (NULL)); for(i=0; i<n;i++) {Array[I] = rand ()% -+1;printf("%d,",Array[i]); }printf("\ n"); Bubble_sort (Array, n);return Array[0];}

It's just a matter of compiling:

errorto‘srand‘errorto‘rand‘

The original is the random number method of the header file into a stdlib, the introduction is possible.
"Android Issue 45: Undefined reference to ' Srand '"
The results of the compilation run are as follows:

$ ~/bin/build-tools/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc-o Test shooter_tester.c SHOOTER.C$ ADB shellroot@hammerhead:/ # CD Data/appRoot@hammerhead:/data/app #./test -, the, the, -, -,Panax Notoginseng, About, -, the, the,A Result:  -Root@hammerhead:/data/app #./test -, -, -, +, -, $, One, -, -, the,A Result:  One
End

Now you can play Android as Linux. Have a nice day!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Combat Tips 44: hello,native!

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.