(ubuntu)在andorid andk工程中使用ccache加速编译速度

本文介绍如何在Linux环境下使用ccache与Android NDK结合,通过配置环境变量和修改makefile文件来实现缓存编译过程,显著提高构建效率。实验证明,在不改变源代码的情况下,ccache能够有效减少编译时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境

系统:Linux luogw-pc 3.5.0-36-generic #57~precise1-Ubuntu SMP Thu Jun 20 18:21:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

ndk:   ndk-r8d

cache: ccache version 3.1.6


第一步:安装ccache

sudo apt-get install ccache


第二步:配置ccache

添加环境变量 export NDK_CCACHE=ccache


第三步:修改 android-ndk-r8e/build/core/default-build-commands.mk

注:红色部份为修改的内容,原内容被注释掉

#
# IMPORTANT: The following definitions must use lazy assignment because
# the value of TOOLCHAIN_PREFIX or TARGET_CFLAGS can be changed later by
# the toolchain's setup.mk script.
#


ifneq ($(findstring ccc-analyzer,$(CC)),)
TARGET_CC       = $(CC)
else
#TARGET_CC       = $(TOOLCHAIN_PREFIX)gcc
TARGET_CC       = ccache $(TOOLCHAIN_PREFIX)gcc
endif
TARGET_CFLAGS   =


ifneq ($(findstring c++-analyzer,$(CXX)),)
TARGET_CXX      = $(CXX)
else
#TARGET_CXX      = $(TOOLCHAIN_PREFIX)g++
TARGET_CXX      = ccache $(TOOLCHAIN_PREFIX)g++
endif
TARGET_CXXFLAGS = $(TARGET_CFLAGS) -fno-exceptions -fno-rtti

实践对比:

使用ccache前后情况对比如下

luogw@luogw-pc:~/study/bitmap-plasma$ ccache -s
cache directory                     /home/luogw/.ccache
cache hit (direct)                     0
cache hit (preprocessed)               0
cache miss                             0
files in cache                         0
cache size                             0 Kbytes
max cache size                       1.0 Gbytes
luogw@luogw-pc:~/study/bitmap-plasma$ time ndk-build
Gdbserver      : [arm-linux-androideabi-4.7] libs/armeabi/gdbserver
Gdbsetup       : libs/armeabi/gdb.setup
Gdbserver      : [arm-linux-androideabi-4.7] libs/armeabi-v7a/gdbserver
Gdbsetup       : libs/armeabi-v7a/gdb.setup
Compile thumb  : plasma <= plasma.c
SharedLibrary  : libplasma.so
Install        : libplasma.so => libs/armeabi/libplasma.so
Compile thumb  : plasma <= plasma.c
SharedLibrary  : libplasma.so
Install        : libplasma.so => libs/armeabi-v7a/libplasma.so

real	0m0.226s
user	0m0.128s
sys	0m0.032s
luogw@luogw-pc:~/study/bitmap-plasma$ ccache -s
cache directory                     /home/luogw/.ccache
cache hit (direct)                     0
cache hit (preprocessed)               0
cache miss                             2
called for link                        2
multiple source files                  2
files in cache                         6
cache size                           108 Kbytes
max cache size                       1.0 Gbytes
luogw@luogw-pc:~/study/bitmap-plasma$ rm -r libs/ obj/
luogw@luogw-pc:~/study/bitmap-plasma$ time ndk-build
Gdbserver      : [arm-linux-androideabi-4.7] libs/armeabi/gdbserver
Gdbsetup       : libs/armeabi/gdb.setup
Gdbserver      : [arm-linux-androideabi-4.7] libs/armeabi-v7a/gdbserver
Gdbsetup       : libs/armeabi-v7a/gdb.setup
Compile thumb  : plasma <= plasma.c
SharedLibrary  : libplasma.so
Install        : libplasma.so => libs/armeabi/libplasma.so
Compile thumb  : plasma <= plasma.c
SharedLibrary  : libplasma.so
Install        : libplasma.so => libs/armeabi-v7a/libplasma.so

real	0m0.114s
user	0m0.052s
sys	0m0.008s
luogw@luogw-pc:~/study/bitmap-plasma$ ccache -s
cache directory                     /home/luogw/.ccache
cache hit (direct)                     2
cache hit (preprocessed)               0
cache miss                             2
called for link                        4
multiple source files                  4
files in cache                         6
cache size                           108 Kbytes
max cache size                       1.0 Gbytes


参考文章:

1)http://www.41post.com/4509/programming/android-use-ccache-with-android-ndk-in-cygwin

2)http://www.mirwing.com/2011/11/android-android-ndk-revision-7.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值