正则表达式pcre在Android下的移植

因为项目需要在android的NDK开发中使用pcre正则表达式库,而android系统中并没有自带该库,所以就得另外移植了, 下面是移植的详细步骤:

 

1. 下载pcre源码,可以到http://sourceforge.net/projects/pcre/下载源码。 
我这里使用的是pcre-7.8.tar.gz 


2. 将pcre-7.8 的源码拷贝至android源码树下的external/pcre目录下。


3. 将下面的Android.mk文件拷贝到external/pcre目录下。

Android.mk:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#
#  Android makefile for libpcre
#
#  This makefile generates libpcre.a, pcregrep and pcre.h ONLY.
#  It should be amended to build libpcreposix.a, libpcrecpp.a
#  and tests.
 
 
LOCAL_PATH := $(call my- dir )
 
###
### Build libpcre.a and pcre.h
###
 
include $(CLEAR_VARS)
 
#step 1:
# to generate the files  config.h pcre.h pcre_chartables.c
#
GEN := $(LOCAL_PATH) /config .h
$(GEN): $(LOCAL_PATH) /config .h.generic
     $(hide) cp  $(LOCAL_PATH) /config .h.generic $@
LOCAL_GENERATED_SOURCES += $(GEN)
 
GEN := $(LOCAL_PATH) /pcre .h
$(GEN): $(LOCAL_PATH) /pcre .h.generic
     $(hide) cp  $(LOCAL_PATH) /pcre .h.generic $@
LOCAL_GENERATED_SOURCES += $(GEN)
 
GEN := $(LOCAL_PATH) /pcre_chartables .c
$(GEN): $(LOCAL_PATH) /pcre_chartables .c.dist
     $(hide) cp  $(LOCAL_PATH) /pcre_chartables .c.dist $@
LOCAL_GENERATED_SOURCES += $(GEN)
 
#step 2:
#clear the vars generated by the step 1
#
include $(CLEAR_VARS)
 
LOCAL_SRC_FILES :=  \
   pcre_compile.c \
   pcre_config.c \
   pcre_dfa_exec.c \
   pcre_exec.c \
   pcre_fullinfo.c \
   pcre_get.c \
   pcre_globals.c \
   pcre_info.c \
   pcre_internal.h \
   pcre_maketables.c \
   pcre_newline.c \
   pcre_ord2utf8.c \
   pcre_refcount.c \
   pcre_study.c \
   pcre_tables.c \
   pcre_try_flipped.c \
   pcre_ucd.c \
   pcre_valid_utf8.c \
   pcre_version.c \
   pcre_xclass.c \
   pcre_chartables.c \
   ucp.h \
   pcre.h \
   config.h
 
#copy the pcre.h to out/target/product/generic/obj/include/libpcre
#
LOCAL_COPY_HEADERS := pcre.h
LOCAL_COPY_HEADERS_TO := libpcre
 
LOCAL_CFLAGS += -O3 -I. -DHAVE_CONFIG_H
 
LOCAL_MODULE := libpcre
LOCAL_MODULE_CLASS := STATIC_LIBRARIES
LOCAL_PRELINK_MODULE := false
 
include $(BUILD_STATIC_LIBRARY)
 
###
### Build pcregrep
###
 
include $(CLEAR_VARS)
 
LOCAL_MODULE := pcregrep
LOCAL_SRC_FILES := pcregrep.c
LOCAL_CFLAGS += -O3 -I. -DHAVE_CONFIG_H
LOCAL_STATIC_LIBRARIES := libpcre
 
include $(BUILD_EXECUTABLE)

4. cd 到 android的源码树的根目录下,输入命令:

. build/envsetup.sh

chooseproduct 
mmm external/pcre 
从下面的log可以看到生成了libpcre.a库:

make: Entering directory `/home/braincol/workspace/android/android_build/android_sdk_froyo' 
Header: out/target/product/generic/obj/include/libpcre/pcre.h 
target thumb C: pcregrep <= external/pcre/pcregrep.c 
target thumb C: libpcre <= external/pcre/pcre_compile.c 
target thumb C: libpcre <= external/pcre/pcre_config.c 
target thumb C: libpcre <= external/pcre/pcre_dfa_exec.c 
target thumb C: libpcre <= external/pcre/pcre_exec.c 
target thumb C: libpcre <= external/pcre/pcre_fullinfo.c 
target thumb C: libpcre <= external/pcre/pcre_get.c 
target thumb C: libpcre <= external/pcre/pcre_globals.c 
target thumb C: libpcre <= external/pcre/pcre_info.c 
target thumb C: libpcre <= external/pcre/pcre_maketables.c 
target thumb C: libpcre <= external/pcre/pcre_newline.c 
target thumb C: libpcre <= external/pcre/pcre_ord2utf8.c 
target thumb C: libpcre <= external/pcre/pcre_refcount.c 
target thumb C: libpcre <= external/pcre/pcre_study.c 
target thumb C: libpcre <= external/pcre/pcre_tables.c 
target thumb C: libpcre <= external/pcre/pcre_try_flipped.c 
target thumb C: libpcre <= external/pcre/pcre_ucd.c 
target thumb C: libpcre <= external/pcre/pcre_valid_utf8.c 
target thumb C: libpcre <= external/pcre/pcre_version.c 
target thumb C: libpcre <= external/pcre/pcre_xclass.c 
target thumb C: libpcre <= external/pcre/pcre_chartables.c 
target StaticLib: libpcre (out/target/product/generic/obj/STATIC_LIBRARIES/libpcre_intermediates/libpcre.a) 
target Executable: pcregrep (out/target/product/generic/obj/EXECUTABLES/pcregrep_intermediates/LINKED/pcregrep) 
target Non-prelinked: pcregrep (out/target/product/generic/symbols/system/bin/pcregrep) 
target Strip: pcregrep (out/target/product/generic/obj/EXECUTABLES/pcregrep_intermediates/pcregrep) 
Install: out/target/product/generic/system/bin/pcregrep 
make: Leaving directory `/home/braincol/workspace/android/android_build/android_sdk_froyo' 

 

然后只要把libpcre.a库和头文件pcre.h拷贝到你的android应用工程中,然后就可以在ndk中使用这个pcre库了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值