Forty-four practical Android skills: Hello, Native!

Source: Internet
Author: User

Forty-four practical Android skills: Hello, Native!

Running C Programs on Android is a little unfamiliar with top-layer apps, because the development of Android apps is still far from Java.
However, for underlying driver developers, this is common because Android is a Linux Branch and the underlying is a world of C/C ++.

Sometimes, to test some functions, we also write C Programs that run directly on the Android terminal. The premise is that you have the root permission for the Android cross compiler and Android system.

Cross-compilation tool

Ndk has done a lot of work for us to develop native programs. Next we will separate the Android cross-compilation tool from 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.binndk$ ./android-ndk-r10e-linux-x86_64.bin

Now, ndk can work. Let's change the cross-compilation tool.

$ ./build/tools/make-standalone-toolchain.sh --platform=android-19 --toolchain=arm-linux-androideabi-4.9Copying prebuilt binaries...Copying sysroot headers and libraries...Copying c++ runtime headers and libraries...Creating package file: /tmp/ndk-linc/arm-linux-androideabi-4.9.tar.bz2Cleaning up...Done.

Find the appropriate path and decompress it:

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

Compile the main_test.c file.

#include 
  
   int main() {    printf(just a test,linc!hello, native!);    return 0;}
  

Compile it:

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

Run:
Copy main_test to Android and run:

$ adb push main_test /data/app137 KB/s (6192 bytes in 0.043s)$ adb shellroot@hammerhead:/ # cd data/app                                               root@hammerhead:/data/app # lsmain_testroot@hammerhead:/data/app # ./main_test                                       just a test,linc!hello, native!

As we wish, the program runs smoothly, just like in Linux. Next we will compile the program for two files.
Shooter. c

#include shooter.h#include 
  
   void bubble_sort(int *array,int n) {    int i,j,tmp;    for(i=0;i
   
    i;j--) {            if(array[j-1]>array[j]) {                tmp = array[j-1];                array[j-1]=array[j];                array[j]=tmp;            }        }    }}int A(int a) {    int n = 10;    int i;    int array[] = {54,12,346,5,23,67,234,324,45,98};        for(i=0;i
    
   
  

shooter_tester.c

#include  #include shooter.hint main() { int result = A(0); printf(A result: %d,result); return 0;} 

Compile and run it:

$ ~/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 (6344 bytes in 0.043s)$ adb shellroot@hammerhead:/ # cd data/app root@hammerhead:/data/app # ./test 54, 12, 346, 5, 23, 67, 234, 324, 45, 98, A result: 5
Use random number

Next, try to test the code in "do a dynamic link library" in Android and try to port this so to the Android platform.
Shooter. c only generates random numbers using rand and srand.

#include shooter.h#include  #include  void bubble_sort(int *array,int n) { int i,j,tmp; for(i=0;i  i;j--) { if(array[j-1]>array[j]) { tmp = array[j-1]; array[j-1]=array[j]; array[j]=tmp; } } }}int A(int a) { int n = 10; int i; int array[n]; srand(time(NULL)); for(i=0;i    

The problem only occurs during compilation:

 error: undefined reference to 'srand' error: undefined reference to 'rand'

The header file of the random number method is changed to stdlib.
45 of Android issues: undefined reference to 'srand'
The compilation and running results 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/app root@hammerhead:/data/app # ./test 18, 95, 91, 55, 13, 37, 74, 85, 83, 66, A result: 13root@hammerhead:/data/app # ./test 59, 100, 84, 32, 26, 46, 11, 50, 44, 83, A result: 11
Conclusion

Now, you can use Android as a Linux player. Have a good time!

 

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.