Friday, July 3, 2009

MinGW: Porting GNU Regex to Windows

I am updating this post with new build information. Rather than using the archaic GNU regex 0.12, we can use the GNU regex package from the MinGW project. Before following instructions here, make sure to install MinGW or MinGW-w64. Then, open the MSYS console. The following commands are meant to be entered in MSYS.

Glibc regex port from MinGW

Download MinGW's regex package from here. The source package I am using is:

I don't recommend getting DLL and DEV packages from the MinGW site because they are not native Windows binaries but MSYS-dependent. Now type the following command to extract the regex source code.

xz -dc regex-1.20090805-2-msys-1.0.13-src.tar.lzma | tar xOf - regex-20090805.tar.xz | xz -dc | tar xvf -

Enter the regex-20090805 directory and run the configure script. If using MinGW-w64, append --build=i686-w64-mingw32 or --build=x86_64-w64-mingw32 to the configure command depending on whether you use 32-bit or 64-bit MinGW-w64 build.

cd regex-20090805
./configure --prefix=/mingw

Then run make.

make
make install

Inside the MinGW folder, you'll find these files: libregex-1.dll, regex.h, libregex.a, libregex.dll.a, libregex.la. Some software expect to find libgnurx.a or libgnurx.dll.a. If so, copy libregex.a to libgnurx.a and libregex.dll.a to libgnurx.dll.a. To use this library, include the following lines in your code:

#define _REGEX_RE_COMP
#include <regex.h>

_REGEX_RE_COMP allows us to use BSD regex functions re_comp and re_exec in addition to POSIX regex functions.

GNU regex 0.12

This section describes how we can use MinGW to compile the standalone GNU regex library 0.12. Be warned that GNU Regex version 0.12 is obsolete and not recommended. I downloaded regex-0.12.tar.gz from http://ftp.gnu.org/old-gnu/regex/ and compiled it as follows:

cd $HOME
tar xzvf regex-0.12.tar.gz
cd regex-0.12/
gcc -DSTDC_HEADERS -DHAVE_STRING_H=1 -I/mingw/include -I. -c regex.c
ar ru libregex.a regex.o
cp libregex.a /mingw/lib
cp regex.h /mingw/include/

If you're using TDM-GCC and have trouble using regex.h, make the following change:

#if defined(VMS) || !defined(_INC_STDDEF)

Using the PCRE library instead

The PCRE library provides another way to implement regex functions on Windows. Read this post for more information.

About This Blog

KBlog logo This blog seeks to provide useful information to people, based on the author's knowledge and experience. Thanks for visiting the blog and posting your comments.

© Contents by KBlog

© Blogger template by Emporium Digital 2008

Follow by Email

Total Pageviews