Building PCRE2 without using autotools

PCRE2 10.50-DEV documentation

This document contains the following sections:

General

The source of the PCRE2 libraries consists entirely of code written in Standard C, and so should compile successfully on any system that has a Standard C compiler and library.

The PCRE2 distribution includes a configure file for use by the configure/make (autotools) build system, as found in many Unix-like environments. The README.md file contains information about the options for configure.

There is also support for building with CMake or Meson. See the sections about these build systems below.

Versions of src/config.h and src/pcre2.h are distributed in the PCRE2 tarballs under the names src/config.h.generic and src/pcre2.h.generic. These are provided for those who build PCRE2 without using configure, CMake, or Meson. These build systems generate the headers instead of using the .generic versions.

Generic instructions for the PCRE2 C libraries

There are three possible PCRE2 libraries, each handling data with a specific code unit width: 8, 16, or 32 bits. You can build any combination of them. The following are generic instructions for building a PCRE2 C library “by hand”. If you are going to use CMake or Meson, this section does not apply to you. Note that the settings concerned with 8-bit, 16-bit, and 32-bit code units relate to the type of data string that PCRE2 processes. They are NOT referring to the underlying operating system bit width. You do not have to do anything special to compile in a 64-bit environment, for example.

  1. Copy or rename the file src/config.h.generic as src/config.h, and edit the macro settings that it contains to whatever is appropriate for your environment. In particular, you can alter the definition of the NEWLINE macro to specify what character(s) you want to be interpreted as line terminators by default. You need to #define at least one of SUPPORT_PCRE2_8, SUPPORT_PCRE2_16, or SUPPORT_PCRE2_32, depending on which libraries you are going to build. You must set all that apply.

    When you subsequently compile any of the PCRE2 modules, you must specify -DHAVE_CONFIG_H to your compiler so that src/config.h is included in the sources.

    An alternative approach is not to edit src/config.h, but to use -D on the compiler command line to make any changes that you need to the configuration options. In this case -DHAVE_CONFIG_H must not be set.

    NOTE: There have been occasions when the way in which certain parameters in src/config.h are used has changed between releases. (In the configure/make world, this is handled automatically.) When upgrading to a new release, you are strongly advised to review src/config.h.generic before re-using what you had previously.

    Note also that the src/config.h.generic file is created from a config.h that was generated by Autotools, which automatically includes settings of a number of macros that are not actually used by PCRE2 (for example, HAVE_DLFCN_H).

  2. Copy or rename the file src/pcre2.h.generic as src/pcre2.h.

  3. EITHER: Copy or rename file src/pcre2_chartables.c.dist as src/pcre2_chartables.c.

    OR: Compile src/pcre2_dftables.c as a stand-alone program (using -DHAVE_CONFIG_H if you have set up src/config.h), and then run it with the single argument src/pcre2_chartables.c. This generates a set of standard character tables and writes them to that file. The tables are generated using the default C locale for your system. If you want to use a locale that is specified by LC_xxx environment variables, add the -L option to the pcre2_dftables command. You must use this method if you are building on a system that uses EBCDIC code.

    The tables in src/pcre2_chartables.c are defaults. The caller of PCRE2 can specify alternative tables at run time.

  4. For a library that supports 8-bit code units in the character strings that it processes, compile the following source files from the src directory, setting -DPCRE2_CODE_UNIT_WIDTH=8 as a compiler option. Also set -DHAVE_CONFIG_H if you have set up src/config.h with your configuration, or else use other -D settings to change the configuration as required.

    pcre2_auto_possess.c
    pcre2_chkdint.c
    pcre2_chartables.c
    pcre2_compile.c
    pcre2_compile_cgroup.c
    pcre2_compile_class.c
    pcre2_config.c
    pcre2_context.c
    pcre2_convert.c
    pcre2_dfa_match.c
    pcre2_error.c
    pcre2_extuni.c
    pcre2_find_bracket.c
    pcre2_jit_compile.c
    pcre2_maketables.c
    pcre2_match.c
    pcre2_match_data.c
    pcre2_match_next.c
    pcre2_newline.c
    pcre2_ord2utf.c
    pcre2_pattern_info.c
    pcre2_script_run.c
    pcre2_serialize.c
    pcre2_string_utils.c
    pcre2_study.c
    pcre2_substitute.c
    pcre2_substring.c
    pcre2_tables.c
    pcre2_ucd.c
    pcre2_valid_utf.c
    pcre2_xclass.c
    

    Make sure that you include -I. in the compiler command (or equivalent for an unusual compiler) so that all included PCRE2 header files are first sought in the src directory under the current directory. Otherwise you run the risk of picking up a previously-installed file from somewhere else.

    Note that you must compile pcre2_jit_compile.c, even if you have not defined SUPPORT_JIT in src/config.h, because when JIT support is not configured, dummy functions are compiled. When JIT support IS configured, pcre2_jit_compile.c #includes other files from the sljit dependency, all of whose names begin with sljit.

    Note also that the pcre2_fuzzsupport.c file contains special code that is useful to those who want to run fuzzing tests on the PCRE2 library. Unless you are doing that, you can ignore it.

  5. Now link all the compiled code into an object library in whichever form your system keeps such libraries. This is the PCRE2 C 8-bit library, typically called something like libpcre2-8. If your system has static and shared libraries, you may have to do this once for each type.

  6. If you want to build a library that supports 16-bit or 32-bit code units, set 16 or 32 as the value of -DPCRE2_CODE_UNIT_WIDTH when obeying step 4 above. If you want to build more than one PCRE2 library, repeat steps 4 and 5 as necessary.

  7. If you want to build the POSIX wrapper functions (which apply only to the 8-bit library), ensure that you have the src/pcre2posix.h file and then compile src/pcre2posix.c. Link the result (on its own) as the pcre2posix library. If targeting a DLL in Windows, make sure to include -DPCRE2POSIX_SHARED with your compiler flags.

  8. The pcre2test program can be linked with any combination of the 8-bit, 16-bit and 32-bit libraries (depending on what you specfied in src/config.h) . Compile src/pcre2test.c; don’t forget -DHAVE_CONFIG_H if necessary, but do NOT define PCRE2_CODE_UNIT_WIDTH. Then link with the appropriate library/ies. If you compiled an 8-bit library, pcre2test also needs the pcre2posix wrapper library when linking.

  9. Run pcre2test on the testinput files in the testdata directory, and check that the output matches the corresponding testoutput files. There are comments about what each test does in the section entitled “Testing PCRE2” in the README.md file. If you compiled more than one of the 8-bit, 16-bit and 32-bit libraries, you need to run pcre2test with the -16 option to do 16-bit tests and with the -32 option to do 32-bit tests.

    Some tests are relevant only when certain build-time options are selected. For example, test 4 is for Unicode support, and will not run if you have built PCRE2 without it. See the comments at the start of each testinput file. The Python script RunTest.py will run the appropriate tests on Unix-like systems and Windows. A Unix shell version is also available as RunTest. The command python3 RunTest.py list (or RunTest list for the shell version) will output a list of all the tests.

    Note that the supplied files are in Unix format, with just LF characters as line terminators.

  10. If you have built PCRE2 with SUPPORT_JIT, the JIT features can be tested by running pcre2test with the -jit option. This is done automatically by RunTest.py and RunTest. You might also like to build and run the freestanding JIT test program, src/pcre2_jit_test.c.

  11. The pcre2test program tests the POSIX wrapper library, but there is also a freestanding test program in src/pcre2posix_test.c. It must be linked with both the pcre2posix library and the 8-bit PCRE2 library.

  12. If you want to use the pcre2grep command, compile and link src/pcre2grep.c; it uses only the 8-bit PCRE2 library (it does not need the pcre2posix library). If you have built the PCRE2 library with JIT support by defining SUPPORT_JIT in src/config.h, you can also define SUPPORT_PCRE2GREP_JIT, which causes pcre2grep to make use of JIT (unless it is run with --no-jit). If you define SUPPORT_PCRE2GREP_JIT without defining SUPPORT_JIT, pcre2grep does not try to make use of JIT.

Stack size in Windows environments

Prior to release 10.30 the default system stack size of 1MiB in some Windows environments caused issues with some tests. This should no longer be the case for 10.30 and later releases.

Linking programs in Windows environments

If you want to statically link a program against a PCRE2 library in the form of a non-dll .a file, you must define PCRE2_STATIC before including src/pcre2.h.

Calling conventions in Windows environments

It is possible to compile programs to use different calling conventions using MSVC. Search the web for “calling conventions” for more information. To make it easier to change the calling convention for the exported functions in a PCRE2 library, the macro PCRE2_CALL_CONVENTION is present in all the external definitions. It can be set externally when compiling (e.g. in CFLAGS). If it is not set, it defaults to empty; the default calling convention is then used (which is what is wanted most of the time).

Comments about Win32 builds

See also Building PCRE2 on Windows with CMake.

There are two ways of building PCRE2 using the configure, make, make install paradigm on Windows systems: using MinGW or using Cygwin. These are not at all the same thing; they are completely different from each other. There is also support for building using CMake, which some users find a more straightforward way of building PCRE2 under Windows.

The MinGW home page (http://www.mingw.org/) says this:

MinGW: A collection of freely available and freely distributable Windows specific header files and import libraries combined with GNU toolsets that allow one to produce native Windows programs that do not rely on any 3rd-party C runtime DLLs.

The Cygwin home page (http://www.cygwin.com/) says this:

Cygwin is a Linux-like environment for Windows. It consists of two parts:

  • A DLL (cygwin1.dll) which acts as a Linux API emulation layer providing substantial Linux API functionality

  • A collection of tools which provide Linux look and feel.

On both MinGW and Cygwin, PCRE2 should build correctly using:

./configure && make && make install

This should create two libraries called libpcre2-8 and libpcre2-posix. These are independent libraries: when you link with libpcre2-posix you must also link with libpcre2-8, which contains the basic functions.

Using Cygwin’s compiler generates libraries and executables that depend on cygwin1.dll. If a library that is generated this way is distributed, cygwin1.dll has to be distributed as well. Since cygwin1.dll is under the GPL licence, this forces not only PCRE2 to be under the GPL, but also the entire application. A distributor who wants to keep their own code proprietary must purchase an appropriate Cygwin licence.

MinGW has no such restrictions. The MinGW compiler generates a library or executable that can run standalone on Windows without any third party dll or licensing issues.

But there is more complication:

If a Cygwin user uses the -mno-cygwin Cygwin gcc flag, what that really does is to tell Cygwin’s gcc to use the MinGW gcc. Cygwin’s gcc is only acting as a front end to MinGW’s gcc (if you install Cygwin’s gcc, you get both Cygwin’s gcc and MinGW’s gcc). So, a user can:

The test files that are supplied with PCRE2 are in UNIX format, with LF characters as line terminators. Unless your PCRE2 library uses a default newline option that includes LF as a valid newline, it may be necessary to change the line terminators in the test files to get some of the tests to work.

Building PCRE2 with Meson

Meson 1.6 or later can configure and build PCRE2 on Linux, macOS, Windows, and other supported platforms. Note that Meson’s own list of supported platforms and compilers is not the same as CMake’s. Meson uses a separate build directory. A normal build, test, and installation can be run as follows:

meson setup build
meson compile -C build
meson test -C build
meson install -C build

For cross-compilation, pass Meson’s standard --cross-file option at setup. Set [binaries] exe_wrapper to a simulator command (such as qemu), and set [properties] needs_exe_wrapper = true if Meson’s automatic detection needs overriding. The main test harness uses MESON_EXE_WRAPPER as its default --sim command; the POSIX and JIT tests are wrapped by Meson itself. As with --sim, wrapper commands for the main harness support whitespace-separated arguments, but not quoting or whitespace within individual arguments. If a required wrapper is not configured, the main harness is omitted and Meson skips the executable tests. Cross builds that can run directly need no wrapper.

PCRE2’s Meson options use lower-case names and can be inspected with meson configure build. For example, the 16-bit and 32-bit libraries and JIT support can be enabled at setup time:

meson setup build -Dpcre2_build_pcre2_16=true \
  -Dpcre2_build_pcre2_32=true -Dpcre2_support_jit=enabled

Meson’s standard default_library option selects static, shared, or both; PCRE2 defaults to a static library. When both variants are built, default_both_libraries selects which one is used by PCRE2’s programs and by an unqualified subproject dependency. Its native default is shared, unlike CMake’s static selection when both variants are built. Meson’s b_staticpic also retains its native default of true, unlike CMake’s PCRE2_STATIC_PIC option. Static and shared PCRE2 libraries are compiled separately so that each receives the correct visibility definitions.

As in CMake, C11 is preferred, with C99 as a fallback. Compiler extensions are enabled where supported; Meson’s c_std option can override this choice.

The optional zlib, bzip2, readline, and libedit dependencies can be found through package metadata or the compiler’s library and header search paths. Readline’s terminal-library dependencies are detected when needed, as in the CMake build. Set the corresponding pcre2_support_lib* feature option to enabled to require a dependency, disabled to omit it, or auto to use it if available. Only one of readline and libedit may be enabled. zlib uses Meson’s built-in dependency finder.

PCRE2 can also be used as a Meson subproject. A parent project can place it in subprojects/pcre2 and request, for example, dependency('libpcre2-8', fallback: 'pcre2'). The corresponding dependency names for the other interfaces are libpcre2-16, libpcre2-32, and libpcre2-posix; Meson’s static keyword can request a particular available library variant. All three build systems install pkg-config files for consumers of installed libraries. Only the CMake build additionally installs CMake package metadata; consumers requiring find_package(PCRE2 CONFIG) should use a CMake-built installation.

Meson generates pkg-config metadata from its library targets, including native thread dependencies when JIT is enabled. In shared and both-library builds, static-link dependencies are reported by pkg-config --static; for static-only builds, Meson includes these in the ordinary link flags too. The --static option does not itself force the linker to select a static library. Build-tree metadata is available in meson-uninstalled without installing the libraries.

On Windows, use a Visual Studio developer command prompt for MSVC (or pass --vsenv to Meson). Static MSVC libraries are named pcre2-8-static.lib, and shared libraries have pcre2-8.dll and pcre2-8.lib; the other widths and POSIX library follow the same convention. Debug builds append d before the extension; debugoptimized builds do not. DLLs and programs install to bindir, while static and import libraries install to libdir. Installed programs find their DLLs in the same directory; other consumers must put bindir on PATH or deploy the DLLs beside their executable.

For MSVC consumers, pkg-config --msvc-syntax provides compiler and linker flags. A static-only installation supplies PCRE2_STATIC and the -static library names. When both variants are installed, the .pc files describe the shared variant, even with --static: that switch neither substitutes the MSVC archive names nor changes the Windows import/export definitions. Use a static-only installation, an explicit Meson subproject dependency with static: true, or manually select the -static libraries and define PCRE2_STATIC (without PCRE2POSIX_SHARED).

Meson installs generated program and DLL PDBs alongside their binaries in debug and debugoptimized builds by default. Unlike CMake’s INSTALL_MSVC_PDB option, this follows Meson’s native policy; -Dstrip=true omits installed PDBs. Static libraries embed their debug information and do not install separate PDBs. The installed pcre2-config script requires a Unix shell; native MSVC consumers should use pkg-config or Meson subproject dependencies instead.

Installed shared-library permissions and symlink layouts follow each build system’s native conventions. On macOS, Meson installs libpcre2-8.0.dylib as the real library with libpcre2-8.dylib linking to it; the other widths and POSIX library follow the same convention. The full current and compatibility versions are recorded in the Mach-O metadata, not the filename. These differences are intentional; packagers may normalize them if required.

Building PCRE2 on Windows with CMake

CMake is an alternative configuration facility that can be used instead of configure. CMake creates project files (make files, solution files, etc.) tailored to numerous development environments, including Visual Studio, Borland, Msys, MinGW, NMake, and Unix. If possible, use short paths with no spaces in the names for your CMake installation and your PCRE2 source and build directories.

If you are using CMake and encounter errors, deleting the CMake cache and restarting from a fresh build may fix the error. In the CMake GUI, the cache can be deleted by selecting “File > Delete Cache”; or the folder CMakeCache can be deleted.

  1. Install the latest CMake version available from http://www.cmake.org/, and ensure that cmake\bin is on your path.

  2. Unzip (retaining folder structure) the PCRE2 source tree into a source directory such as C:\pcre2. You should ensure your local date and time is not earlier than the file dates in your source dir if the release is very new.

  3. Create a new, empty build directory, preferably a subdirectory of the source dir. For example, C:\pcre2\pcre2-xx\build.

  4. Run CMake.

    • Using the CLI, simply run cmake .. inside the build/ directory. You can use the ccmake ncurses GUI to select and configure PCRE2 features.

    • Using the CMake GUI:

      1. Run cmake-gui from the Shell environment of your build tool, for example, Msys for Msys/MinGW or Visual Studio Command Prompt for VC/VC++.

      2. Enter C:\pcre2\pcre2-xx and C:\pcre2\pcre2-xx\build for the source and build directories, respectively.

      3. Press the “Configure” button.

      4. Select the particular IDE / build tool that you are using (Visual Studio, MSYS makefiles, MinGW makefiles, etc.)

      5. The GUI will then list several configuration options. This is where you can disable Unicode support or select other PCRE2 optional features.

      6. Press “Configure” again. The adjacent “Generate” button should now be active.

      7. Press “Generate”.

  5. The build directory should now contain a usable build system, be it a solution file for Visual Studio, makefiles for MinGW, etc. Exit from cmake-gui and use the generated build system with your compiler or IDE. E.g., for MinGW you can run make, or for Visual Studio, open the PCRE2 solution, select the desired configuration (Debug, or Release, etc.) and build the ALL_BUILD project.

    Regardless of build system used, cmake --build . will build it.

  6. If during configuration with cmake-gui you’ve elected to build the test programs, you can execute them by building the test project. E.g., for MinGW: make test; for Visual Studio build the RUN_TESTS project. The most recent build configuration is targeted by the tests. A summary of test results is presented. Complete test output is subsequently available for review in Testing\Temporary under your build dir.

    Regardless of build system used, ctest will run the tests.

Testing with Python

The autotools build uses make check to run the test suites using the RunTest and RunGrepTest shell (bash) scripts. The test suite is also available for non-Unix platforms using the equivalent RunTest.py and RunGrepTest.py Python scripts instead.

These Python scripts can be used with any build system, and should work on supported PCRE2 platforms.

The autotools build only uses the shell scripts.

The CMake build tests for a Python 3 installation, and defaults to registering the Python scripts with CTest. If Python 3 is not found, CMake registers the shell scripts on Unix, or fails otherwise.

On Windows, the requirement for a Python installation to run the test suite can be avoided by passing -DPCRE2_BUILD_TESTS=OFF, although releasing untested builds is not recommended.

After building, run CTest in the usual way, specifying the configuration for a multi-configuration generator. For example:

cmake --build build --config Release
ctest --test-dir build -C Release --output-on-failure

The scripts can also be run directly from the build directory. If the source and executable paths cannot be inferred from that directory, pass them explicitly. For example:

python3 ../RunTest.py --srcdir .. --pcre2test ./Release/pcre2test.exe
python3 ../RunGrepTest.py --srcdir .. ^
  --pcre2test ./Release/pcre2test.exe ^
  --pcre2grep ./Release/pcre2grep.exe

The runner scripts (shell and Python) support out-of-tree builds by accepting commandline arguments specifying the location of the srcdir (the directory containing the testdata/ directory); of pcre2test[.exe]; and for RunGrepTest and RunGrepTest.py, pcre2grep[.exe]. Alternatively, environment variables can be used to pass the paths; and if none are provided the working directory is used.

Building PCRE2 on z/OS and z/VM

z/OS and z/VM are operating systems for mainframe computers, produced by IBM. The character code used is EBCDIC, not ASCII or Unicode. In z/OS, UNIX APIs and applications can be supported through UNIX System Services.

The PCRE2 codebase compiles and runs with native EBCDIC support on modern z/OS systems, using the pre-installed tools (/bin/sh, ./configure, and the XLC or IBM-Clang compilers). PCRE2 supports z/OS using both Autoconf (./configure) and CMake (which IBM distributes via zopen install cmake).

Note that as of the time of writing, IBM’s port of CMake to z/OS has only partial support for EBCDIC. It is recommended to build PCRE2 using the ./configure script, if you require an EBCDIC build.

Any EBCDIC codepage should work (PCRE2 does not assume or require IBM-1047), or PCRE2 can compiled for ASCII/Latin-1/Unicode.

After unpacking the PCRE2 tarball, you must subsequently tag the files as ASCII in order for the z/OS shell and compiler to interpret them correctly:

chtag -R -tc ISO8859-1 <directory>

Some unusual features on the IBM platform are:

In native z/OS (without UNIX System Services) and in z/VM, a user has provided a special port of PCRE2. For details, please see file 939 on this web site:

http://www.cbttape.org

The user-provided port also provides an API for LE languages such as COBOL and for the z/OS and z/VM versions of the Rexx languages.

Building PCRE2 under VMS

Alexey Chuphin has contributed some auxiliary files for building PCRE2 under OpenVMS. They are in the vms directory in the distribution tarball. Please read the file called vms/openvms_readme.txt. The pcre2test and pcre2grep programs contain some VMS-specific code.

This has not been tested for some time. The PCRE2 maintainers would be grateful to learn whether it still works (or if anyone still uses it).


Last updated: 17 October 2025