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

来源:互联网 发布:大護法影评知乎 编辑:程序博客网 时间:2024/05/17 01:12
正则表达式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库了。

原创粉丝点击