android下使用google test入门

最近在研究android下的C++单元测试,使用google test进行了摸索实践,在此分享一下如何修改makefile 编译生成android下可执行程序,并在android上执行查看结果,更深入的细节未曾涉及。

首先介绍一下我使用的开发环境,Linux Mint 14,android-ndk-r8d,android 2.3.6系统的三星GT9001手机。(后续操作均是在此开发环境下进行,大家需要根据自己的开发环境作一些调整)

下面依次对各步骤进行描述。

下载google test 1.6源码解压修改Makefile

我的解压目录为/home/cfh/ndk/source/gtest,然后我将 gtest/make 目录下的Makefile进行了修改(该Makefile以google test 提供sample1为演示对象,建议先将原始的Makefile进行备份,以便后续参考!),修改的总体思路是将Makefile中默认的编译变量改为NDK对应的值(如将 CXX 设置为 arm-linux-androideabi-g++),设置好编译选项,包括宏定义(定义ANDROID宏用于编译android使用的gtest)、搜索的头文件路径、系统头文件根路径(--sysroot设置)、链接的静态库。

最终的Makefile内容如下所示,我使用的NDK目录(ANDROID_NDK)为/home/cfh/ndk,大家需要根据自己系统的设置进行调整。

# A sample Makefile for building Google Test and using it in user
# tests.  Please tweak it to suit your environment and project.  You
# may want to move it to your project's root directory.
#
# SYNOPSIS:
#
#   make [all]  - makes everything.
#   make TARGET - makes the given target.
#   make clean  - removes all files generated by make.

# Please tweak the following variable definitions as needed by your
# project, except GTEST_HEADERS, which you can use in your own targets
# but shouldn't modify.

# Points to the root of Google Test, relative to where this file is.
# Remember to tweak this if you move this file.
GTEST_DIR := $(shell pwd)/..
$(info $(GTEST_DIR))

# Where to find user code.
USER_DIR = $(GTEST_DIR)/samples

ANDROID_NDK = /home/cfh/ndk
ANDROID_NDK_BIN_DIR = ${ANDROID_NDK}/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin
CC = $(ANDROID_NDK_BIN_DIR)/arm-linux-androideabi-gcc
CXX = $(ANDROID_NDK_BIN_DIR)/arm-linux-androideabi-g++

# Flags passed to the preprocessor.
CPPFLAGS += -DANDROID \
            -I$(GTEST_DIR)/include \
            -I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/4.6/include \
            -I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi/include \
            --sysroot=$(ANDROID_NDK)/platforms/android-14/arch-arm

NDK_LD_PATH = $(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi
LDFLAGS = $(NDK_LD_PATH)/libgnustl_static.a \
          $(NDK_LD_PATH)/libsupc++.a

# Flags passed to the C++ compiler. (backup: -fexceptions -frtti)
CXXFLAGS += -g -Wall -Wextra

# All tests produced by this Makefile.  Remember to add new tests you
# created to the list.
TESTS = sample1_unittest

# All Google Test headers.  Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h

# House-keeping build targets.
.PHONY : all clean
all : $(TESTS)

clean :
	rm -f $(TESTS) gtest.a gtest_main.a *.o

# Builds gtest.a and gtest_main.a.

# Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized.  This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
gtest-all.o : $(GTEST_SRCS_)
	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o : $(GTEST_SRCS_)
	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest_main.cc

gtest.a : gtest-all.o
	$(AR) $(ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
	$(AR) $(ARFLAGS) $@ $^

# Builds a sample test.  A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function.

sample1.o : $(USER_DIR)/sample1.cc $(USER_DIR)/sample1.h $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1.cc

sample1_unittest.o : $(USER_DIR)/sample1_unittest.cc \
                     $(USER_DIR)/sample1.h $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1_unittest.cc

sample1_unittest : sample1.o sample1_unittest.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ $(LDFLAGS) -o $@

进入 gtest/make 所在目录运行make进行编译链接

cfh@cfh ~/ndk/sources/gtest/make $ make

将编译生成的可执行文件push到设备或模拟器上

可执行文件放到android设备或模拟器的 /data/local 目录下是可以通过adb shell 正常运行的,其它目录则可能存在问题,如/sdcard

cfh@cfh ~/ndk/sources/gtest/make $ adb push ./sample1_unittest /data/local

使用adb shell执行目标程序

cfh@cfh ~/ndk/sources/gtest/make $ adb shell
# cd /data/local
# ./sample1_unittest

执行结果如下

Running main() from gtest_main.cc
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (2 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (6 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative (0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial (0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (1 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (14 ms total)
[  PASSED  ] 6 tests.

至此,在android下使用google test全流程走通,其它高级或复杂的需求可以在此基础上继续实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值