MasterofProject

Android forty-four: Native, Hello!

Label NativeAndroidcross compileBottomC language
4975 people read comments(0) Collection Report
Classification:

In the Android run the C program to do some strange upper App shoes, because the development of the current Android application still around but Java.
But for driver developers, this is because Android is homely food, Linux branch, the underlying C/C++ world.

Sometimes in order to test some of the features, we also write the C program directly running on the Android terminal. The premise is that there are Android cross compiler and Android system root permissions.

Cross compiler tool

NDK has done a lot of work for our development of the native program, we will be 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).

Chmod a ndk$+Android x-ndk-r10e-linux-x86_64.Bin
Ndk$./android-ndk-r10e-linux-x86_64.Bin

At this point, NDK will be able to work. Let's turn the cross compiler tool.

$./build/tools/make-standalone-toolchain.Sh-Platform=Android-Nineteen -Toolchain=Arm-linux-androideabi-Four point nine
Prebuilt binaries Copying...
SysRoot headers CopyingAndLibraries...
C Copying+ +Headers runtimeAndLibraries...
Package file: /tmp/ndk Creating-linc/arm-linux-androideabi-Four point nine.Tar.Bz2
Up Cleaning...
Done.

To find the right path, extract:

Tar jxvf arm-linux-androideabi- build-tools$Four point nine.tar.bz2
Arm-linux-androideabi-Four point nine/
...

Native, hello

Compile C file main_test.c


<stdio.h> #include

IntMain () {
    Printf("A test Linc, just, Hello, native! \n");
    Return Zero;
}

Compile it:

$~/bin/build-tools/arm-linux-androideabi-Four point nine/bin/arm-linux-androideabi-gcc -oMain_test main_test.C

Run up:
Main_test copy to run Android:

$Push main_test /data/app ADB
One hundred and thirty-seven KB(/sSix thousand one hundred and ninety-twoBytesIn Zero.043S)
$Shell ADB
Root@hammerhead: / # CD data/app
Root@hammerhead/data/app # LS
Main_test
Root@hammerhead/data/app #./main_test
A test Linc, just, Hello, native!

As we wish, the program runs smoothly, as in the Linux system. Here we compile the program of two files.
Shooter.c

Shooter.h "#include"
<stdio.h> #include

VoidBubble_sort (Int*Array,IntN) {
    IntI, J, tmp;
    For(i=ZeroI<n-;OneI++) {
        For(j=n-One; j>i; j--) {
            If(Array[j-One]>Array[j]) {
= TMPArray[j-One];
                Array[j-One]=Array[j];
                Array[j]=tmp;
}
}
}
}

Int(AIntA) {
    Int= nTen;
    IntI;
    Int Array[] = {Fifty-four,Twelve,Three hundred and forty-six,Five,Twenty-three,Sixty-seven,Two hundred and thirty-four,Three hundred and twenty-four,Forty-five,Ninety-eight};
    For(i=Zero; i<n; i++) {
        Printf("%d,",Array[i]);
}

    Printf("\n");
Bubble_sort (ArrayN);
    Return Array[Zero];
}

Shooter_tester.c

<stdio.h> #include
Shooter.h "#include"

IntMain () {
    IntResult = A (Zero);
Printf ("Result:%d\n A"Result);
    Return Zero;
}

Compile and run:

$~/bin/build-tools/arm-linux-androideabi-Four point nine-o test shooter_tester.c shooter.c /bin/arm-linux-androideabi-gcc
$Push test /data/app/ ADB
One hundred and forty-three KB(/sSix thousand three hundred and forty-fourBytesIn Zero.043S)
$Shell ADB
Root@hammerhead: / # CD data/app
Root@hammerhead/data/app #./test
Fifty-four,Twelve,Three hundred and forty-six,Five,Twenty-three,Sixty-seven,Two hundred and thirty-four,Three hundred and twenty-four,Forty-five,Ninety-eight,
A Result: Five

Using random numbers

Next try to"Make a dynamic link library"In the code in the Android test, ready to transplant the so to the Android platform.
Shooter.c just to generate random numbers with Rand and srand above.

Shooter.h "#include"
<time.h> #include
<stdio.h> #include

VoidBubble_sort (Int*Array,IntN) {
    IntI, J, tmp;
    For(i=ZeroI<n-;OneI++) {
        For(j=n-One; j>i; j--) {
            If(Array[j-One]>Array[j]) {
TMP =阵列[—1];
                阵列[—1=阵列[ ];
                阵列[ J ] = TMP;
}
}
}
}

国际的一(国际的一){
    国际的=10国际的我;
    国际的 阵列[氮];

srand(time(空));
    对于(我=0我,我+ +){
        阵列[我] = rand() %100+1printf“%,”阵列[我]);
}

    printf“\”);
bubble_sort(阵列,氮);
    返回 阵列0];
}

只是编译的时候遇到了问题:

 误差未定义的引用 “站”
 误差未定义的引用 “兰德”

原来是随机数方法的头文件变成了提要,引入就可以了。
《Android问题集锦之四十五:未定义的“》srand
编译运行结果如下:

美元~/桶/建工具/ ARM Linux androideabi—4.9/斌/ ARM Linux androideabi GCC - O试验shooter_tester射手。C。C
美元亚洲开发银行
根@锤头/ # CD数据/应用程序@锤头/数据/应用程序 # /测试。
18959155133774858366一个 结果: 13@锤头/数据/应用程序 # /测试。
591008432264611504483一个 结果: 11

尾声

现在,可以把Android当成玩了祝你愉快Linux!

3
0
猜你在找
查看评论
*以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2520092次
    • 积分:30836
    • 等级:
    • 排名:90名第
    • 原创:550篇
    • 转载:315篇
    • 译文:24篇
    • 评论:878条
    博主介绍
    大叔的年龄小伙子的心,喜欢折腾,对自己感兴趣的事物有一种痴迷劲儿,不弄出个所以然来是不会轻易放手的年龄大再加上事情多,非全速前进不可了。
    博客专栏
    文章分类
    Latest comments