• Android学习——NDK交叉编译


    原创作品,转载请注明出处,严禁非法转载。如有错误,请留言!

    email:40879506@qq.com

     一. 环境
    1.GNU/Linux Ubuntu12.04操作系统(x86)

    二. 下载NDK安装包

    下载NDK,根据NDK里docs文档里的standalone-toolchain.html来抽取交叉编译的环境。

    三. 制作交叉编译工具链

    配置环境变量末尾添加如下三行

    vi /etc/profile

    1 export NDK=/opt/cross_tool/android-ndk-r10d
    2 export NDK_CROSS=/opt/cross_tool/AndroidToolChain/bin
    3 PATH=$PATH:$NDK:$NDK_CROSS

     保存后重启环境

    source /etc/profile

    执行命令:$NDK/build/tools/make-standalone-toolchain.sh --platform=android-19 --arch=arm --install-dir=/opt/cross_tool/AndroidToolChain/

    1 # $NDK/build/tools/make-standalone-toolchain.sh --platform=android-19 --arch=arm --install-dir=/opt/cross_tool/AndroidToolChain/
    2 Auto-config: --toolchain=arm-linux-androideabi-4.8
    3 Copying prebuilt binaries...
    4 Copying sysroot headers and libraries...
    5 Copying c++ runtime headers and libraries...
    6 Copying files to: /opt/cross_tool/AndroidToolChain/
    7 Cleaning up...
    8 Done.

    如上图说明已经部署好交叉编译环境。

    其中$NDK环境变量是NDK的安装路径,选项--platform指定Android版本的开发形式,对应版本4.4.2 API。--arch指定目标执行的架构。--install-dir指定这个新生成的文件夹即是你的交叉编译环境,和其他交叉编译工具链使用方法类似。

    四. 编写测试程序和编译

    编译C程序hello.c

    1 #include <stdio.h>
    2 int main()
    3 {
    4     printf("hello,JNI!!!! Android 4.4
    ");
    5     return 0;
    6 }

    使用命令:

    arm-linux-androideabi-gcc hello.c -o hello

    即生成了可在手机arm里运行的可执行文件hello。

    五. 使用Window下的模拟机测试

    把hello程序拷贝到Android的SDK平台下的测试目录,我这里创建了一个test目录。(你也可以配置window下的环境变量,在任意目录下使用adb命令)

    用adb命令把hello程序放到模拟机的/system/bin目录并执行。测试图如下:

  • 相关阅读:
    【371】Twitter 分类相关
    【370】Python列表生成式(for 写入一行)
    Netbeans 中的编译器相关配置
    SP*
    du 命令
    闲杂
    Shell重定向&>file、2>&1、1>&2的区别
    Shell编程中Shift的用法
    shell中一维数组值得获取
    shell expr的用法
  • 原文地址:https://www.cnblogs.com/myblesh/p/6639964.html
Copyright © 2020-2023  润新知