目录
1. 前置条件
2. 编译pcre2生成静态链接库
2.1 编译体系及工具链选择
2.2 编译参数配置
3. 库的使用
3.1 库的添加
3.2 头文件的添加
3.3 代码实现
4. 库和头文件分享
1. 前置条件
windows 8 64位机器,已经安装meson 、ninja 、mingw gcc8.1.0版本、cmake。
以上四个工具网上很容易下载到:
1)meson和ninja 在同一个包里面,在meson官网可以下载,目前采用了0.53.1的版本。
2) mingw64 在sourceforge下载
3) cmake 官网下载。
pcre2采用的版本 pcre2-10.34 ,直接从官网下载
2. 编译pcre2生成静态链接库
2.1 编译体系及工具链选择
命令行下运行cmake-gui,选择编译的源码和build目录,其中build目录为自己新建
D:\meson\test>cmake-gui
选择Configure,弹出 编译体系选项及工具链选择,此处使用ninja编译,本地默认的工具链

工具链探测相关内容:
The C compiler identification is GNU 8.1.0
Check for working C compiler: C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe
Check for working C compiler: C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Could NOT find BZip2 (missing: BZIP2_LIBRARIES BZIP2_INCLUDE_DIR)
Found ZLIB: C:/Program Files (x86)/Acer/abFiles/zlib1.dll (found version "1.2.5")
Could not find OPTIONAL package Readline
Could not find OPTIONAL package Editline
Looking for dirent.h
Looking for dirent.h - found
Looking for stdint.h
Looking for stdint.h - found
Looking for inttypes.h
Looking for inttypes.h - found
Looking for sys/stat.h
Looking for sys/stat.h - found
Looking for sys/types.h
Looking for sys/types.h - found
Looking for unistd.h
Looking for unistd.h - found
Looking for windows.h
Looking for windows.h - found
Looking for bcopy
Looking for bcopy - not found
Looking for memmove
Looking for memmove - found
Looking for strerror
Looking for strerror - found
2.2 编译参数配置
首先采用默认参数生成静态库。更改参数可以生成动态库。目前采用静态库方式。
选择编译工具后,会默认选择工具选项,不想更改的话,可以用默认的,如下。

而后再次选择Configure,配置编译参数后,选择Generate,即可以生成编译体系。编译参数的内容如下:
Could NOT find BZip2 (missing: BZIP2_LIBRARIES BZIP2_INCLUDE_DIR)
Could not find OPTIONAL package Readline
Could not find OPTIONAL package Editline PCRE2 configuration summary:
Install prefix .................. : C:/Program Files (x86)/PCRE2
C compiler ...................... : C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe
C compiler flags ................ : -ID:/meson/pcre2-10.34/src Build 8 bit PCRE2 library ....... : ON
Build 16 bit PCRE2 library ...... : OFF
Build 32 bit PCRE2 library ...... : OFF
Enable JIT compiling support .... : OFF
Use SELinux allocator in JIT .... : OFF
Enable Unicode support .......... : ON
Newline char/sequence ........... : LF
\R matches only ANYCRLF ......... : OFF
\C is disabled .................. : OFF
EBCDIC coding ................... : OFF
EBCDIC coding with NL=0x25 ...... : OFF
Rebuild char tables ............. : OFF
Internal link size .............. : 2
Parentheses nest limit .......... : 250
Heap limit ...................... : 20000000
Match limit ..................... : 10000000
Match depth limit ............... : MATCH_LIMIT
Build shared libs ............... : OFF
Build static libs ............... : ON
Build pcre2grep ................. : ON
Enable JIT in pcre2grep ......... : ON
Enable callouts in pcre2grep .... : ON
Enable callout fork in pcre2grep. : ON
Buffer size for pcre2grep ....... : 20480
Build tests (implies pcre2test .. : ON
and pcre2grep)
Link pcre2grep with libz ........ : ON
Link pcre2grep with libbz2 ...... : Library not found
Link pcre2test with libeditline . : Library not found
Link pcre2test with libreadline . : Library not found
Support Valgrind .................: OFF
Use %zu and %td ..................: AUTOConfiguring done
Generating done
生成的编译体系如下图:

而后运行编译命令,生成库文件和可执行文件
D:\meson\pcre2-10.34>ninja -C build

更改配置参数,可以生成动态库。

可以看到多了两个dll了

3. 库的使用
两种方式,可以直接使用pcre的接口,此外该库也封装了posix兼容的接口
官方说明为,POSIX 兼容的接口:http://pcre.org/current/doc/html/pcre2posix.html
我们采用posix兼容的接口,这样代码改动最小。
3.1 库的添加
本文采用mingw64的编译工具链,将第二章中生成的两个静态库 libpcre2-8.a 和libpcre2-posix.a拷贝到工具链的目录下

当然也可以链接时加库的目录。个人以为拷贝到工具链下更合适
3.2 头文件的添加
将头文件pcre2posix.h添加到工具链的include目录中,实际测试pcre2.h不需要添加。

3.3 代码实现
经过上述两步,就可以在代码中使用此正则库了:
1)链接编译配置,增加两个库的链接参数
meson的build文件内容为
project('test','c')
#regex=files('regcomp.c','regex.c','regex_internal.c','regexec.c')
link_args=['-lpcre2-posix','-lpcre2-8']
executable('hellomeson','hellomeson.c',link_args:link_args)2)头文件 包含 #include "pcre2posix.h"
在实际编译时,还存在:
hellomeson@exe/hellomeson.c.obj: In function `matchRegex':
D:\meson\test\build/../hellomeson.c:11: undefined reference to `__imp_pcre2_regc
omp'
D:\meson\test\build/../hellomeson.c:18: undefined reference to `__imp_pcre2_rege
xec'
D:\meson\test\build/../hellomeson.c:28: undefined reference to `__imp_pcre2_regf
ree'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
实际链接参数已经添加了相应的库 : LINK_ARGS = "-Wl,--allow-shlib-undefined" "-static" "-Wl,--start-group" "-lpcre2-posix" "-lpcre2-8" "-mconsole"
3) 在源代码开始处增加: #define PCRE2_STATIC
上述链接时报错的解决方法:
在源代码开始处增加: #define PCRE2_STATIC,由于采用了PCRE,这里使用#define PCRE2_STATIC,而非#define PCRE_STATIC,增加后即可编译链接通过。完整的代码如下:
#define PCRE2_STATIC
#include <stdio.h>
#include "pcre2posix.h"
int matchRegex(const char* pattern, const char* userString)
{
int result = 0;
regex_t regex;
int regexInit = pcre2_regcomp(®ex, pattern, REG_EXTENDED);
if( regexInit )
{
//Error print : Compile regex failed
}
else
{
int reti = pcre2_regexec( ®ex, userString, 0, NULL, 0 );
if( 0 != reti )
{
//Error print: match failed!
printf("no match\n");
result=1;
}
else
{
result = 0;
}
}
pcre2_regfree( ®ex );
return result;
}
int main(void)
{
int ret=0;
ret=matchRegex("hello","helloworld");
printf("match ?if 0 (%d)\n",ret);
printf("==========================\n");
ret=matchRegex("hehe","helloworld");
printf("match ?if 0 (%d)\n",ret);
}
4. 库和头文件分享
便于直接在windows 64位下使用,不用再重新编译。