Search code examples
c++c++11visual-c++pcre

Using pcre2 in a c++ project


I am looking at using pcre2 in my simple c++ app, (I am using vs2015). (I am looking at various regex libraries and the general feeling is that pcre/pcre2 are the most flexible)

First I downloaded pcre2 from the official location, (http://sourceforge.net/projects/pcre/files/pcre2/10.20/) and created a very simple example.

#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
...
PCRE2_SPTR subject = (PCRE2_SPTR)std::string("this is it").c_str();
PCRE2_SPTR pattern = (PCRE2_SPTR)std::string("([a-z]+)|\\s").c_str();

...
int errorcode;
PCRE2_SIZE erroroffset;
pcre2_code *re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 
                                PCRE2_ANCHORED | PCRE2_UTF, &errorcode,  
                                &erroroffset, NULL);
...

First of all the file "pcre2.h" does not exist, so I renamed pcre2.h.generic to pcre2.h

But then I get linker errors with unresolved externals.

I am guessing I need to include one or more files from the source to project. But I am reluctant to just randomly add files without knowing what it all does.

Can someone give some simple steps to follow to successfully build a project using pcre2?

UPDATE
This is not an import library issue, pcre2.h does not come with a librar, (not one that I can see in their release location).


Solution

  • In case someone wants to build the library using visual studio

    1. Download pcre2 from the website, (http://www.pcre.org/)
    2. in Visual Studio 2015, (and maybe others), create an empty project "Win32 project" and call it pcre2.
    3. Copy all the files in \pcre2\src\ to your newly created empty project.
    4. Add all the files listed in "NON-AUTOTOOLS-BUILD", (located in the base folder)
      • pcre2_auto_possess.c
      • pcre2_chartables.c
      • pcre2_compile.c
      • pcre2_config.c
      • etc...
    5. Rename the file config.h.generic to config.h
    6. Add the config.h file to the project.
    7. In your project, select all the *.c file Go Properties > C/C++ > Precompiled Header > "Not Using Precompiled header"
    8. Select the project, Go to Properties > Preprocessor > Preprocessor Definition and select the drop down list, and add...
      • PCRE2_CODE_UNIT_WIDTH=8
      • HAVE_CONFIG_H

    Compile and the lib file should be created fine.