In this post, I want to provide a short tutorial on how to integrate RE2 regular expression engine in your C++ application in Visual Studio.

Visual Studio files generation

  • First of all, download the RE2 sources from GitHub. Since September 2015 a CMakeLists.txt  was added to support builds with CMake.
  • Download and install CMake (under Binary distributions → Platform section choose Windows installer). CMake is a collection of tools for building, testing and packaging software. Among all its features it can generate Visual Studio project and solution files, so you can get a regular VS project.
    • In CMake-Gui specify the path to the location of  RE2 CMakeLists.txt file (it must be at the root of the downloaded sources) and where to build the binaries (typically in the same as root directory under ./compile subfolder).
      CMake configure paths
    • Click “Configure” button and in a pop-up window select the Visual Studio version you want to generate your solution for.
    • After Configuring is done, you can select the options for generation. In my case, I’ve chosen not to build shared libs and testing; CMake configuration types left only Debug and Release; removed install prefix.
      CMake Generate Properties
  • Click “Generate” button. After the generation is done, you can find .sln and .proj files in “compile” folder.

Finally you can open RE2.sln in Visual Studio and build it. ALL_BUILD project is just there for purpose of building all projects. If you need only a re2.lib file, it will be located in  .\compile\”Configuration” folder. Another option is to add RE2 sources to your project. Motivation behind and how to do it is explained in the next section.

Integration in your C++ application

For some projects, you may want to add RE2 in your solution with corresponding sources, so that you can debug it, distribute your application as a single binary file. This may also include security reasons, so that nobody will spoof it with “evil” re2.lib. And for open-source applications it just makes the perfect sense to have all the sources clear and available to fork/clone and build without downloading any additional dependencies and solving problems with versions compatibility.

Let’s get started.

  • Create a folder, where you will place the RE2 sources. I would recommend the following folders structure: “MySolutionFolder”\3dParty\re2Project. And by the way, do not forget to update 3rd-party projects once in a while😊.
    For the RE2 itself (without test projects) you will need only a small subset of the downloaded files.
  • Copy from sources “re2” and “util” folders and from .\compile re2.vcxproj and re2.vcxproj.filters.
  • We will need to adjust paths from absolute to relative and alter build output and intermediate directories. Open re2.vcxproj file in Notepad++ and replace:
    • A content of all OutDir and IntDir tags to “$(Configuration)\”
    • All occurrences of your download source folder absolute path (f.e. in my case F:\Projects\re2) to “.” (dot for current directory).
      • Delete the whole item groups which starts like:
        <ItemGroup>
        <CustomBuild Include=”.\CMakeLists.txt”>

        </ItemGroup>
        and
        <ItemGroup>
        <ProjectReference Include=”mypath/compile/ZERO_CHECK.vcxproj”>

        </ItemGroup>

In re2.vcxproj.filters replace absolute pahts with relative as well.

  • Right click on your solution in Visual Studio→ Add→ Existing project and select re2.vcxproj.
  • Right click on the project where you want to use RE2 and open project Properties dialog window.
    • Go to Common Properties→ References and add a reference to your newly added re2 solution project.
    • Go to Configuration Properties→ C/C++→ General and fill in Additional Include Directories  re2 folder path (f.e. ..\3dParty\re2Project\re2).
  • The last tweak is to adjust an include path to stringpiece.h in re2.h header from “re2/stringpiece.h” to just  “stringpiece.h”
  • Rebuild the solution.

You can go further and delete files and folders which seem like only for testing purposes (fuzzing and testing folders, make_perl_groups.pl, a bunch of python files etc.) in order to keep your repository clean.

That’s it. Now you can use RE2 in your own application.

Related posts:
Using RE2 with UTF-16 encoded strings

How to build and integrate RE2 in your C++ project for Windows
Tagged on:                         
newest oldest most voted
Notify of