views:

276

answers:

3

All I need is get MinGW talking to Postgres. I've considered several options:

  1. Use libpq. The libpq.lib that comes with Postgres for Windows links okay, but crashes when I use the library. I think because it was compiled for VC++. I can't find just the libpq code, so I'd have to recompile the entire Postgres tree in MinGW. Not easy.
  2. Use libpqxx. Requires libpq!
  3. Use libodbcxx. Requires some sort of ODBC manager like iodbc. Maybe I can use iodbc directly. Since this is just Windows for now can I use -lodbc on my linker and use some Windows specific commands?

Option 1 sounds the least painful. I'm pretty sure the project will use Postgres, not too sure if the project will stay on Windows. Is there a simple way to get this functionality?

+3  A: 

You can rebuild just libpq if you have to. Run "./configure" and then run "make" in just src/interfaces/libpq.

But really, the msvc built libpq should work just fine with mingw. It's just a standard Windows DLL. It may be an issue with the .lib - but the DLL should be fine. AFAIK, only the PQtrace() functionality will be broken - because of the way MSVCRT works, that only works if you have the exact same version of the runtime.

You should also be able to generate an import library directly off the DLL, if the .LIB doesn't work. Or does MingW even allow you to link directly to the DLL - see their wiki

Getting libpq to work is likely a lot easier than odbc.

Magnus Hagander
+1 for mentioning that libpq should work just fine with MinGW. That's true as long as both programs are linked against the same version of C standard library.
Filip Navara
Actually, they don't have to have the same version of the C standard library. A few API calls, like PQtrace, requires it, but most should be fine without that.
Magnus Hagander
It was much easy than it sounded to even build all of Postgres. I was thinking it needed to be built in an IDE. Windows can use the "./configure", "make", "make install" in MSYS! Very cool. Why would anyone choose VC++ over MinGW?
User1
BTW: It was crashing because the libpq.dll was not in the deploy directory. Anyone have a good way to copy those automatically?
User1
A: 

As you also asked R questions here, I assume you can manage to read source to R packages. The RPostgreSQL package does exactly what you are after:

  • It uses MinGW as all R binaries and packages on Windows are built with MinGW
  • It links to libpq to talk to Postgres

The file src/Makevars.win simply has

PKG_CPPFLAGS=-I"${PG_HOME}"/include
PKG_LIBS=-L"${PG_HOME}"/lib -llibpq

which tells R where the Pg headers and library are, and then links with libpq.

Dirk Eddelbuettel
A: 

A side-note... Your CXXFLAGS and LDFLAGS for compiling and linking to the Postgres files can be setup easily using the following variables in your Makefile:

CXXFLAGS += -I$(shell pg_config --includedir)
LDFLAGS += -L$(shell pg_config --libdir) -llibpq

(Provided you have included the bin folder that contains pg_config.exe of your PostgreSQL installation on your system's PATH and that you use mingw32-make.)

Peter Jansson