Saturday, May 5, 2012

How to statically link PCRE to your app in Windows with GCC

I usually face many problems when trying to statically link *some* library to my application, but thank god I almost always find a solution.

In order to statically link PCRE to your application in Windows using GCC, you must have a static version of it.
When you compile PCRE you usually end up with a libpcre-1.dll,libpcre.a,libpcre.dll.a files. You obviously wish to use LIBPCRE.A, which is the static version of PCRE.

You need to add this macro before you include pcre.h in your application:

#define PCRE_STATIC 1

So it looks like this:
#define PCRE_STATIC 1
#include <pcre.h>

After this, in your makefile, command line, Dev-CPP,Code::Blocks or whatever build options you need to add the following line:
-Wl,-Bstatic -lpcre

In case -lpcre is *NOT* the last library you include you may need to write it like so
-Wl,-Bstatic -lpcre -Wl,-Bdynamic
in order to ensure, that the linker doesn't try to statically link whatever libraries follow

No comments:

Post a Comment