hbpgsql for Harbour with MinGW64Step-by-step recipe for compiling the Harbour PostgreSQL contrib as a static library (libhbpgsql.a) against a modern libpq, without installing MSYS2.
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.
hbpgsql.hbx and tpostgre.prg, generated the import library liblibpq.a from libpq.dll, and produced libhbpgsql.a.
The MSYS2 package is just a compressed archive; you can download and unpack it directly.
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.)
.pkg.tar.zst with 7-Zip version 24 or newer (older 7-Zip cannot read the .zst format). This gives you a .tar file..tar with 7-Zip again. Inside you will find a mingw64 tree with:
mingw64\include: headers, including libpq-fe.hmingw64\lib: static and import libraries (libpq.dll.a, libpq.a)mingw64\bin: the runtime DLL libpq-5.dllThe 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)
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.Set the HB_WITH_PGSQL environment variable to the include folder:
set HB_WITH_PGSQL=E:\hbpgsql\include
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.
libhbpgsql.a to your project: either with -L pointing at its folder and -lhbpgsql, or by including hbpgsql.hbc in your own .hbp.liblibpq.a).libpq.dll must sit next to your executable, together with its own dependencies (OpenSSL, libintl, and others, which live in separate MSYS2 packages).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.
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
where gcc lists more than one gcc, or one that is not your 64-bit MinGW, put your mingw64\bin folder first in the PATH.gcc --print-prog-name=cc1 prints a path, check that cc1.exe actually exists there (it normally lives under libexec\gcc\x86_64-w64-mingw32\<version>).build_hbpgsql), which invokes hbmk2 with a known-good toolchain. The specific root cause of the cc1 failure was not confirmed.| Item | Value |
|---|---|
| Harbour | 3.2.1dev (hbmk2) |
| Compiler | MinGW 64-bit (-comp=mingw64) |
| libpq source | MSYS2 mingw-w64-x86_64-postgresql package, unpacked with 7-Zip 24+ |
| Layout | include\ and lib\ side by side; libpq.dll inside lib\ |
| Environment | HB_WITH_PGSQL = path to include\ |
| Build command | hbmk2 hbpgsql.hbc hbpgsql.hbp -comp=mingw64 |
| Output | libhbpgsql.a (static) + liblibpq.a (import) |
| Runtime | libpq.dll + its dependencies next to the exe; smoke test pending |