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:                         

27 thoughts on “How to build and integrate RE2 in your C++ project for Windows

  • March 8, 2019 at 5:13 pm
    Permalink

    Some truly nice and utilitarian info on this internet site, as well I think the design contains excellent features.

    Reply
  • May 2, 2019 at 3:10 pm
    Permalink

    This piece of writing provides clear idea in support of the new people of blogging, that really how to do running a blog.

    Reply
  • May 3, 2019 at 1:01 pm
    Permalink

    I blog often and I seriously appreciate your information. Your
    article has really peaked my interest. I will
    take a note of your blog and keep checking for new details about once per week.
    I opted in for your RSS feed as well.

    Reply
  • May 4, 2019 at 1:10 am
    Permalink

    Hey There. I found your blog using msn. This is an extremely
    well written article. I will make sure to bookmark it and return to read more of your useful information. Thanks for the post.
    I will certainly comeback.

    Reply
  • May 5, 2019 at 3:45 pm
    Permalink

    Thanks for your personal marvelous posting! I seriously enjoyed reading it,
    you might be a great author. I will always bookmark your blog and will often come back down the road.

    I want to encourage that you continue your great posts,
    have a nice evening!

    Reply
  • May 6, 2019 at 7:32 am
    Permalink

    I have been exploring for a little bit for any high-quality articles or blog posts in this sort of space .
    Exploring in Yahoo I at last stumbled upon this site.

    Reading this info So i am satisfied to show that I have an incredibly excellent uncanny feeling I came upon exactly what I needed.

    I most without a doubt will make sure to do not disregard this website
    and provides it a look regularly.

    Reply
  • May 7, 2019 at 1:26 am
    Permalink

    I’m not sure why but this blog is loading extremely slow for me.
    Is anyone else having this problem or is it a issue on my end?

    I’ll check back later on and see if the problem still exists.

    Reply
  • May 8, 2019 at 11:14 pm
    Permalink

    Asking questions are in fact pleasant thing if you are not understanding anything totally, however this paragraph provides good understanding even.

    Reply
  • May 9, 2019 at 7:20 pm
    Permalink

    Fine way of explaining, and pleasant article to obtain data on the topic of my presentation focus, which i am going to convey in institution of higher education.

    Reply
  • May 9, 2019 at 11:36 pm
    Permalink

    Hi there everyone, it’s my first go to see at this website,
    and post is really fruitful designed for me, keep up posting
    these types of articles or reviews.

    Reply
  • May 10, 2019 at 9:20 pm
    Permalink

    Have you ever thought about adding a little bit more than just your articles?
    I mean, what you say is fundamental and everything.

    Nevertheless imagine if you added some great graphics or
    videos to give your posts more, “pop”! Your content is excellent
    but with pics and videos, this site could definitely be one of
    the most beneficial in its niche. Excellent blog!

    Reply
  • May 11, 2019 at 3:37 am
    Permalink

    I’m really enjoying the design and layout of
    your site. It’s a very easy on the eyes which makes it
    much more enjoyable for me to come here and visit more often. Did you hire out
    a designer to create your theme? Fantastic work!

    Reply
  • May 12, 2019 at 12:39 am
    Permalink

    It is really a great and helpful piece of info. I am glad that you just shared this
    useful info with us. Please stay us up to date like this.

    Thank you for sharing.

    Reply
  • May 12, 2019 at 7:06 am
    Permalink

    I am really impressed together with your writing talents as well as with the structure to your blog.
    Is that this a paid subject matter or did you modify it
    your self? Either way keep up the excellent quality writing, it’s uncommon to see a nice blog like this one today..

    Reply
  • May 13, 2019 at 12:42 am
    Permalink

    Hello, I believe your website may be having browser compatibility
    problems. Whenever I take a look at your web site in Safari,
    it looks fine however, when opening in I.E., it has some overlapping issues.

    I merely wanted to provide you with a quick heads up! Other than that, excellent site!

    Reply
  • May 13, 2019 at 7:11 am
    Permalink

    Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your next write ups thank you once again.

    Reply
  • May 13, 2019 at 5:08 pm
    Permalink

    Hey very nice blog!! Guy .. Excellent .. Wonderful ..
    I’ll bookmark your blog and take the feeds additionally?
    I am glad to search out numerous useful information here within the put up,
    we want work out more techniques on this regard, thank you for sharing.
    . . . . .

    Reply
  • May 14, 2019 at 10:50 pm
    Permalink

    I am sure this piece of writing has touched all the internet users,
    its really really good post on building up new web site.

    Reply
  • May 17, 2019 at 2:37 am
    Permalink

    I am now not sure the place you’re getting your info,
    however good topic. I must spend some time studying much more
    or figuring out more. Thank you for fantastic information I used to be on the lookout for this info for my mission.

    Reply
  • May 17, 2019 at 3:27 am
    Permalink

    Hello outstanding blog! Does running a blog such
    as this require a massive amount work? I’ve virtually no understanding of programming however I had been hoping to start my own blog in the near future.
    Anyhow, if you have any ideas or techniques for new blog owners please share.
    I know this is off topic nevertheless I just wanted to ask.

    Thanks a lot!

    Reply
  • May 17, 2019 at 4:50 am
    Permalink

    I’m really impressed with your writing skills and also with the
    layout on your weblog. Is this a paid theme or did you
    modify it yourself? Either way keep up the excellent quality writing, it’s rare to see a nice blog like this one nowadays.

    Reply

Leave a Reply

Your email address will not be published.