You are here

Building and installing libqxx libraries on Win32 machine

shroman's picture

Build libpq.dll and libpqdll.lib interface libraries.

Get postgresql sources .tar.gz

Extract archive into e.g.: E:\PGPROG\postgresql-8.1.3

Run VS.NET command prompt

Change working directory src of the extracted source tree

cd E:\PGPROG\postgresql-8.1.3\src

Run win32 make file:

nmake.exe /f win32.mak

On completion you should see something like “All Win32 parts have been built!”

Next step is building libpqxx

Unzip source archive into e.g.: E:\PGPROG\libpqxx-2.5.5

Edit win32/common file to reflect changes in postgresql library paths

Exampole common file listing:

!IF "$(OS)" == "Windows_NT"





#-STL options

STD=/D "PGSTD=std"

# If you are using a 3rd party STL like STLport, remember to check your path

# and be sure the STLport is included before the MSVC includes. VCVARS32.BAT

# doesn't know anything about the directories as defined in the IDE. I use

# set INCLUDE=\Utility\Code\STLport-4.5\stlport;%INCLUDE%


# Depending on your STL library min/max need to be defined. Using STLport

# there is no need for libpqxx to redefine these for me.

# The next line gives us the src directory to add additional include directories

# We need certain header files from the Postgres code. Specifically

# src/include/c.h

# src/include/config.h

# src/include/postgres_ext.h

# src/include/postgres_fe.h

# src/interfaces/libpq/libpq-fe.h


# If the LIBPQ library is not in the LIB environment or search path specified it here


Create win32 configuration headers include/pqxx/config-*-*.h. by copying all from

\config\sample-headers\compiler\VisualC++.NET-2003 and

\config\sample-headers\libpq\8.0 into include/pqxx/.

Open VC.NET command prompt and change directory to: E:\PGPROG\libpqxx-2.5.5\win32

Run nmake /f libpqxx.mak ALL to build all static and dynamic libraries

"nmake /f test.mak ALL" will compile and link all the test files. It is currently set to use the DLL version of libpqxx.

{ To direct the regression test to the right database, set some or all of the

following environment variables as needed before running “nmake /f test.mak ALL”:

PGDATABASE (name of database; defaults to your user name)

PGHOST (database server; defaults to local machine)

PGPORT (PostgreSQL port to connect to; default is 5432)

PGUSER (your PostgreSQL user ID; defaults to your login name

PGPASSWORD (password used for tests)}

Creating Visual Studio project, running MFC with libpqxx

Set Project properties to All Configurations:

C++ general options(Include Directories)


Set Linker properties (Additional libraries)


In your source file use following include declarations:


#include <pqxx/pqxx>

#include <cassert>

using namespace PGSTD;

using namespace pqxx;

The following example shows howto create a connection and execute transaction



// Set up connection to database

string ConnectString = ("host=ipaddr dbname=testdb user=username password=mypasswd");

connection C(ConnectString);

// Start transaction within context of connection

work T(C,"tx");

// Perform query within transaction

result R( T.exec("SELECT * FROM alfalinks.t_main") );



for (result::size_type i = 0; i < R.size(); ++i)


//Add test table column Nr.4 text value into a treeview(MFC)




catch (const sql_error &e)


CString tmp;

tmp = e.query().c_str();



catch (const exception &e)


CString tmp;

tmp = e.what();



catch (...)


AfxMessageBox(_T("Unknown error!!!"),0,0);


Functions for convertion of UTF-8 to Windows UNICODE


string toNarrowString( const wchar_t* pStr , int len )


ASSERT( len >= 0 || len == -1 , _T("Invalid string length: ") << len ) ;

// figure out how many narrow characters we are going to get

int nChars = WideCharToMultiByte( CP_UTF8 , 0 ,

pStr , len , NULL , 0 , NULL , NULL ) ;

if ( len == -1 )

nChars ;

if ( nChars == 0 )

return "" ;

string buf ;

buf.resize( nChars ) ;

WideCharToMultiByte( CP_UTF8 , 0 , pStr , len ,

const_cast<char*>(buf.c_str()) , nChars , NULL , NULL ) ;

return buf ;


wstring toWideString( const char* pStr , int len )


ASSERT( len >= 0 || len == -1 , _T("Invalid string length: ") << len ) ;

// figure out how many wide characters we are going to get

int nChars = MultiByteToWideChar( CP_UTF8 , 0 , pStr , len , NULL , 0 ) ;

if ( len == -1 )

nChars ;

if ( nChars == 0 )

return L"" ;

wstring buf ;

buf.resize( nChars ) ;

MultiByteToWideChar( CP_UTF8 , 0 , pStr , len ,

const_cast<wchar_t*>(buf.c_str()) , nChars ) ;

return buf ;