Ubuntu上通过android toolchain交叉编译Valgrind操作步骤

Posted fengbingchun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu上通过android toolchain交叉编译Valgrind操作步骤相关的知识,希望对你有一定的参考价值。

      关于Valgrind的介绍可以参考:https://blog.csdn.net/fengbingchun/article/details/50196189.

      这里介绍下在Ubuntu 16.04上通过android toolchain如android-ndk-r14b交叉编译Valgrind源码的操作步骤:

      1. 从 https://valgrind.org/ 下载最新的版本3.17.0,解压缩,并将终端定位到主目录;

      2. 从官网https://developer.android.com/ndk/downloads/older_releases#ndk-14b-downloads下载android-ndk-r14b并解压缩;

      3. 编写脚本build_android.sh,内容如下:此脚本既支持armv7也支持aarch64,在valgrind主目录下执行此脚本

#! /bin/bash

if [ $# != 1 ]; then
    echo "usage: $0 system_architect"
    echo "e.g: build armv7: $0 0; build aarch64: $0 1"
    exit 1
fi

if [ $1 != 0 -a $1 != 1 ]; then
    echo "input param can only be 0 or 1: \\"$0 0\\" or \\"$0 1\\""
    exit 1
fi

toolchain_path=${HOME}/Disk/Soft/android-ndk-r14b
echo "toolchain path: ${toolchain_path}"

if [ $1 == 0 ]; then
    echo "build android armv7"

    export AR=${toolchain_path}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar
    export LD=${toolchain_path}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld
    export CC=${toolchain_path}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
    export CXX=${toolchain_path}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++

    export RANLIB=${toolchain_path}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ranlib
    export STRIP=${toolchain_path}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-strip

    bash autogen.sh

    CPPFLAGS="--sysroot=${toolchain_path}/platforms/android-21/arch-arm" \\
    CFLAGS="--sysroot=${toolchain_path}/platforms/android-21/arch-arm" \\
    ./configure \\
        --prefix=/ \\
        --host=armv7-unknown-linux \\
        --target=armv7-unknown-linux \\
        --with-tmpdir=/sdcard

    make -j4
    make install DESTDIR=`pwd`/install_armv7
fi

if [ $1 == 1 ]; then
    echo "build android aarch64"

    export AR=${toolchain_path}/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-ar
    export LD=${toolchain_path}/ttoolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-ld
    export CC=${toolchain_path}/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-gcc
    export CXX=${toolchain_path}/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-g++

    export RANLIB=${toolchain_path}/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-ranlib
    export STRIP=${toolchain_path}/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip

    bash autogen.sh

    CPPFLAGS="--sysroot=${toolchain_path}/platforms/android-21/arch-arm64" \\
    CFLAGS="--sysroot=${toolchain_path}/platforms/android-21/arch-arm64" \\
    ./configure \\
        --prefix=/ \\
        --host=aarch64-unknown-linux \\
        --target=aarch64-unknown-linux \\
        --with-tmpdir=/sdcard

    make -j4
    make install DESTDIR=`pwd`/install_aarch64
fi

      4. 解决编译时的error:oredump-elf.c:149:4: error: conflicting types for 'Elf32_Nhdr',修改源码coregrind/m_coredump/coredump-elf.c,将文件中结构体名Elf32_Nhdr调整为Elf32_Nhdr_,此结构体好像并没有用到

      5. 执行完脚本后,会在valgrind主目录下生成install_armv7和install_aarch64目录,结构如下:bin目录下存放着valgrind可执行文件,libexec/valgrind目录下存放着所需的动态库。注意:(1).valgrind的旧版本动态库的存放位置是lib/valgrind目录;(2).在编译完armv7或aarch64后,如继续编译另一个架构,需先执行make clean,再执行./build_android.sh 0或./build_android.sh 1

       6. 将install_armv7或install_aarch64拷贝到手机上,查看valgrind版本,结果如下:可见valgrind执行正确。注意:(1).新版本valgrind的VALGRIND_LIB为./install_aarch64/libexec/valgrind,在旧版本中为./install_aarch64/lib/valgrind;(2).当测试的可执行文件依赖其它动态库时,需要用LD_LIBRARY_PATH指定依赖动态库所在的目录;(3).如在执行过程中,遇到类似以下错误时"valgrind: Can't create client cmdline file in /data/local/tmp/valgrind_proc_xxxx_cmdline_yyyyyyyy",在原有命令基础上,添加TMPDIR=. 即可,如TMPDIR=. VALGRIND_LIB=./install_aarch64/libexec/valgrind LD_LIBRARY_PATH=target/android-aarch64/ ./install_aarch64/bin/valgrind --leak-check=full --log-file=log.txt ./build/CppBaseTest

 

 

以上是关于Ubuntu上通过android toolchain交叉编译Valgrind操作步骤的主要内容,如果未能解决你的问题,请参考以下文章

在Ubuntu17.04上搭建Android Studio开发环境

在 Ubuntu 上使用 ADB 备份 Android 数据

怎样在 Ubuntu 上安装 Linux Kernel 4.4

怎么在ubuntu上编译android源码

手机秒变PC!手把手教你给爱机刷入Ubuntu Touch系统

ubuntu14.04上搭建android开发环境