Building hbpgsql for Harbour with MinGW64

Step-by-step recipe for compiling the Harbour PostgreSQL contrib as a static library (libhbpgsql.a) against a modern libpq, without installing MSYS2.

Overview

This recipe produces a static Harbour library, libhbpgsql.a, built with Harbour 3.2.1dev (hbmk2) and a 64-bit MinGW toolchain, linking against the libpq client library from the MSYS2 PostgreSQL package. The hbpgsql source already adapts to the libpq version at compile time (via PG_VERSION_NUM), so current libpq versions (16 through 18) are accepted by the code.

Verified Steps 1 through 4 reproduce a build that completed successfully on the author's machine: hbmk2 compiled hbpgsql.hbx and tpostgre.prg, generated the import library liblibpq.a from libpq.dll, and produced libhbpgsql.a.

1Get libpq without installing MSYS2

The MSYS2 package is just a compressed archive; you can download and unpack it directly.

  1. Download the package file from the MSYS2 mirror, for example:
    https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-postgresql-18.7-2-any.pkg.tar.zst
    (The current file name is linked from the package page: packages.msys2.org/packages/mingw-w64-x86_64-postgresql.)
  2. Extract the .pkg.tar.zst with 7-Zip version 24 or newer (older 7-Zip cannot read the .zst format). This gives you a .tar file.
  3. Open the .tar with 7-Zip again. Inside you will find a mingw64 tree with:
    • mingw64\include: headers, including libpq-fe.h
    • mingw64\lib: static and import libraries (libpq.dll.a, libpq.a)
    • mingw64\bin: the runtime DLL libpq-5.dll

2Create the folder layout the build expects

The hbpgsql project file looks for libpq in a lib folder that sits next to the include folder. The MSYS2 package puts the DLL in bin, so copy the pieces into this layout (example uses E:\hbpgsql; any root folder works):

E:\hbpgsql\
  include\    <-- full contents of mingw64\include
  lib\        <-- the .a files from mingw64\lib
              plus libpq-5.dll copied here and renamed to libpq.dll
  hbpgsql.hbp, hbpgsql.hbc, ... (the contrib sources)
Why the rename: the project file references the DLL as libpq.dll, while the package ships it as libpq-5.dll. Copying it into lib\ under the name libpq.dll lets hbmk2 generate its import library automatically.

3Point Harbour at the headers

Set the HB_WITH_PGSQL environment variable to the include folder:

set HB_WITH_PGSQL=E:\hbpgsql\include

4Build with hbmk2

From the project folder, run hbmk2 with the contrib project files and the 64-bit MinGW compiler:

hbmk2 hbpgsql.hbc hbpgsql.hbp -comp=mingw64

A successful build prints lines like these:

hbmk2: Dependencia 'pgsql' encontrada: E:\hbpgsql\include
hbmk2: Biblioteca de importación creada: liblibpq.a <= E:\hbpgsql\lib\libpq.dll
Compiling 'hbpgsql.hbx'...
Compiling 'tpostgre.prg'...
hbmk2: Creando librería estática... libhbpgsql.a

The result is libhbpgsql.a, plus the generated import library liblibpq.a for the PostgreSQL client DLL.

5Using the library in your programs

  1. Add libhbpgsql.a to your project: either with -L pointing at its folder and -lhbpgsql, or by including hbpgsql.hbc in your own .hbp.
  2. Link the PostgreSQL import library as well (the generated liblibpq.a).
  3. At runtime, libpq.dll must sit next to your executable, together with its own dependencies (OpenSSL, libintl, and others, which live in separate MSYS2 packages).
Not verified yet Linking a final application and running a live connection test (for example with TPQServer) were not part of the verified build. Run a quick connection test before integrating the library into production programs; if Windows reports a missing DLL at startup, download the matching dependency package from the same MSYS2 mirror.

Troubleshooting: gcc: cannot exec 'cc1'

This error is a toolchain problem, not a PostgreSQL or hbpgsql problem: gcc.exe starts but cannot find cc1.exe, the actual C compiler. The usual causes are a PATH that picks up a stray gcc.exe from another program, or an incomplete or moved MinGW installation.

Run these in the same console where you build:

where gcc
gcc -v
gcc --print-prog-name=cc1
In the session this guide is based on, the cleanest fix was to run the build through the author's own build script (build_hbpgsql), which invokes hbmk2 with a known-good toolchain. The specific root cause of the cc1 failure was not confirmed.

Summary

ItemValue
Harbour3.2.1dev (hbmk2)
CompilerMinGW 64-bit (-comp=mingw64)
libpq sourceMSYS2 mingw-w64-x86_64-postgresql package, unpacked with 7-Zip 24+
Layoutinclude\ and lib\ side by side; libpq.dll inside lib\
EnvironmentHB_WITH_PGSQL = path to include\
Build commandhbmk2 hbpgsql.hbc hbpgsql.hbp -comp=mingw64
Outputlibhbpgsql.a (static) + liblibpq.a (import)
Runtimelibpq.dll + its dependencies next to the exe; smoke test pending