前段时间为了能够在c++里使用正则库,特地去网上下了个pcre的源码包,费了好大力气终于实现了可以将pcre在win32项目里将其作为静态库和动态库调用,今天正好抽空分享下。
首先,在网上下载pcre源码包,我下的是pcre-8.34.zip,解压后其文件缩略图如下
其实源码包里有详细说明手动编译pcre正则库的步骤,pcre-8.34\doc\html\NON-AUTOTOOLS-BUILD.txt,这里附上我参考的原文部分:
GENERIC INSTRUCTIONS FOR THE PCRE C LIBRARY
The following are generic instructions for building the PCRE C library "by
hand". If you are going to use CMake, this section does not apply to you; you
can skip ahead to the CMake section.
(1) Copy or rename the file config.h.generic as config.h, and edit the macro
settings that it contains to whatever is appropriate for your environment.
In particular, you can alter the definition of the NEWLINE macro to
specify what character(s) you want to be interpreted as line terminators.
In an EBCDIC environment, you MUST change NEWLINE, because its default
value is 10, an ASCII LF. The usual EBCDIC newline character is 21 (0x15,
NL), though in some cases it may be 37 (0x25).
When you compile any of the PCRE modules, you must specify -DHAVE_CONFIG_H
to your compiler so that config.h is included in the sources.
An alternative approach is not to edit config.h, but to use -D on the
compiler command line to make any changes that you need to the
configuration options. In this case -DHAVE_CONFIG_H must not be set.
NOTE: There have been occasions when the way in which certain parameters
in config.h are used has changed between releases. (In the configure/make
world, this is handled automatically.) When upgrading to a new release,
you are strongly advised to review config.h.generic before re-using what
you had previously.
(2) Copy or rename the file pcre.h.generic as pcre.h.
(3) EITHER:
Copy or rename file pcre_chartables.c.dist as pcre_chartables.c.
OR:
Compile dftables.c as a stand-alone program (using -DHAVE_CONFIG_H if
you have set up config.h), and then run it with the single argument
"pcre_chartables.c". This generates a set of standard character tables
and writes them to that file. The tables are generated using the default
C locale for your system. If you want to use a locale that is specified
by LC_xxx environment variables, add the -L option to the dftables
command. You must use this method if you are building on a system that
uses EBCDIC code.
The tables in pcre_chartables.c are defaults. The caller of PCRE can
specify alternative tables at run time.
(4) Ensure that you have the following header files:
pcre_internal.h
ucp.h
(5) For an 8-bit library, compile the following source files, setting
-DHAVE_CONFIG_H as a compiler option if you have set up config.h with your
configuration, or else use other -D settings to change the configuration
as required.
pcre_byte_order.c
pcre_chartables.c
pcre_compile.c
pcre_config.c
pcre_dfa_exec.c
pcre_exec.c
pcre_fullinfo.c
pcre_get.c
pcre_globals.c
pcre_jit_compile.c
pcre_maketables.c
pcre_newline.c
pcre_ord2utf8.c
pcre_refcount.c
pcre_string_utils.c
pcre_study.c
pcre_tables.c
pcre_ucd.c
pcre_valid_utf8.c
pcre_version.c
pcre_xclass.c
Make sure that you include -I. in the compiler command (or equivalent for
an unusual compiler) so that all included PCRE header files are first
sought in the current directory. Otherwise you run the risk of picking up
a previously-installed file from somewhere else.
Note that you must still compile pcre_jit_compile.c, even if you have not
defined SUPPORT_JIT in config.h, because when JIT support is not
configured, dummy functions are compiled. When JIT support IS configured,
pcre_jit_compile.c #includes sources from the sljit subdirectory, where
there should be 16 files, all of whose names begin with "sljit".
(6) Now link all the compiled code into an object library in whichever form
your system keeps such libraries. This is the basic PCRE C 8-bit library.
If your system has static and shared libraries, you may have to do this
once for each type.
(7) If you want to build a 16-bit library (as well as, or instead of the 8-bit
or 32-bit libraries) repeat steps 5-6 with the following files:
pcre16_byte_order.c
pcre16_chartables.c
pcre16_compile.c
pcre16_config.c
pcre16_dfa_exec.c
pcre16_exec.c
pcre16_fullinfo.c
pcre16_get.c
pcre16_globals.c
pcre16_jit_compile.c
pcre16_maketables.c
pcre16_newline.c
pcre16_ord2utf16.c
pcre16_refcount.c
pcre16_string_utils.c
pcre16_study.c
pcre16_tables.c
pcre16_ucd.c
pcre16_utf16_utils.c
pcre16_valid_utf16.c
pcre16_version.c
pcre16_xclass.c
(8) If you want to build a 32-bit library (as well as, or instead of the 8-bit
or 16-bit libraries) repeat steps 5-6 with the following files:
pcre32_byte_order.c
pcre32_chartables.c
pcre32_compile.c
pcre32_config.c
pcre32_dfa_exec.c
pcre32_exec.c
pcre32_fullinfo.c
pcre32_get.c
pcre32_globals.c
pcre32_jit_compile.c
pcre32_maketables.c
pcre32_newline.c
pcre32_ord2utf32.c
pcre32_refcount.c
pcre32_string_utils.c
pcre32_study.c
pcre32_tables.c
pcre32_ucd.c
pcre32_utf32_utils.c
pcre32_valid_utf32.c
pcre32_version.c
pcre32_xclass.c
(9) If you want to build the POSIX wrapper functions (which apply only to the
8-bit library), ensure that you have the pcreposix.h file and then compile
pcreposix.c (remembering -DHAVE_CONFIG_H if necessary). Link the result
(on its own) as the pcreposix library.
(10) The pcretest program can be linked with any combination of the 8-bit,
16-bit and 32-bit libraries (depending on what you selected in config.h).
Compile pcretest.c and pcre_printint.c (again, don't forget
-DHAVE_CONFIG_H) and link them together with the appropriate library/ies.
If you compiled an 8-bit library, pcretest also needs the pcreposix
wrapper library unless you compiled it with -DNOPOSIX.
(11) Run pcretest on the testinput files in the testdata directory, and check
that the output matches the corresponding testoutput files. There are
comments about what each test does in the section entitled "Testing PCRE"
in the README file. If you compiled more than one of the 8-bit, 16-bit and
32-bit libraries, you need to run pcretest with the -16 option to do
16-bit tests and with the -32 option to do 32-bit tests.
Some tests are relevant only when certain build-time options are selected.
For example, test 4 is for UTF-8/UTF-16/UTF-32 support, and will not run
if you have built PCRE without it. See the comments at the start of each
testinput file. If you have a suitable Unix-like shell, the RunTest script
will run the appropriate tests for you. The command "RunTest list" will
output a list of all the tests.
Note that the supplied files are in Unix format, with just LF characters
as line terminators. You may need to edit them to change this if your
system uses a different convention. If you are using Windows, you probably
should use the wintestinput3 file instead of testinput3 (and the
corresponding output file). This is a locale test; wintestinput3 sets the
locale to "french" rather than "fr_FR", and there some minor output
differences.
(12) If you have built PCRE with SUPPORT_JIT, the JIT features will be tested
by the testdata files. However, you might also like to build and run
the freestanding JIT test program, pcre_jit_test.c.
(13) If you want to use the pcregrep command, compile and link pcregrep.c; it
uses only the basic 8-bit PCRE library (it does not need the pcreposix
library).
- 创建一个win32空项目,不要预编译头,类型我选择的是静态库,工程创建好之后也可以选择编译为动态库;
- 将config.h.generic、pcre.h.generic、pcre_chartables.c.dist改为config.h、pcre.h、pcre_chartables.c;
- 将上一步的二个头文件及pcre_internal.h、ucp.h这四个头文件一起拷贝到工程目录下添加到项目当中;
- 将NON-AUTOTOOLS-BUILD.txt文档当中的第五步所列的20个.c文件和第2步的pcre_chartables.c文件一起拷贝到工程目录中,并添加至工程;
- 工程属性——配置属性——c++——预处理器,在预处理器定义中添加HAVE_CONFIG_H宏定义;
- 为了全工程能够正常通过编译,需要修改config.h头文件,进行如下修改:
1、定义SUPPORT_UTF宏,config.h里默认是/* #undef SUPPORT_UTF */,将其修改为:#define SUPPORT_UTF 1
不然使用时,若在测试工程中是用unicode字符集将出现错误:argument is not a compiled regular expression
#ifndef PCRE_STATIC
#define PCRE_STATIC 1
#endif
最后,测试工程中添加PCRE_STATIC定义
将以下代码添加到测试工程当中即可
#include "stdafx.h"
#include "pcre.h"
#include <string.h>
int _tmain(int argc, _TCHAR* argv[])
{
char pattern[]="\\d+";
char content[]="owaefj ifea 894 aewoai 43";
const char* error;
int erroffset;
int cur=0;
int ovector[30];
pcre* re=pcre_compile(pattern,PCRE_MULTILINE|PCRE_NO_AUTO_CAPTURE|PCRE_UTF8,
&error,&erroffset,NULL);
pcre_extra* pcEx=pcre_study(re,PCRE_STUDY_JIT_COMPILE,&error);
int rc=pcre_exec(re,pcEx,content,strlen(content),cur,PCRE_NOTEMPTY,ovector,30);
if(rc>=0)
{
char* ptr;
ptr=content+ovector[1];
*ptr='\0';
printf("%s\n",content+ovector[0]);
}
pcre_free(re);
system("pause");
return 0;
}
编译运行,