libpq not working - Mailing list pgsql-cygwin

From Stefan Nobis
Subject libpq not working
Date
Msg-id 87vgb3pu7z.fsf@520075220525-0001.dialin.t-online.de
Whole thread Raw
Responses Re: libpq not working
List pgsql-cygwin
Hi.

I tried to play a little bit with libpq on windows (the postgresql
server is running on a Debian/GNU Linux box, version 7.1.3).

So i eventually compiled libpq with mingw (i got the Makefiles from
Gerhard Häring and modified them a little bit (in 7.1.3 there is no
md5.c or i didn't find it)). Then i tried to comile and run the
following program:

------------------------
#include <stdio.h>
#include <libpq-fe.h>

int main() {
  PGconn *con = NULL;

  con = PQconnectdb("host=mauzi.home hostaddr=10.0.1.2 port=5432 dbname=testdb user=xxxx password=xxxx");
  if (con == NULL) {
    puts("ERROR: con is NULL!");
    return 0;
  }
  if (PQstatus(con) == CONNECTION_BAD)
    printf ("Connection failed!\n %s\n", PQerrorMessage(con));
  else
    puts("Everything is OK! Connected.");

  PQfinish(con);
  return 0;
}
------------------------

On Linux everything works fine and i got "Everything is OK!
Connected." But on windows i got the following (compiler commands
included):

---------------------------
C:\Eigene Dateien\dbtest>make
gcc -g -Wall -ansi -pedantic -Ic:/dev/inc -I.   -c -o t.o t.c
gcc -g -Lc:/dev/lib -o t.exe t.o -lpq -lwsock32

C:\Eigene Dateien\dbtest>t
Connection failed!
 connectDBStart() -- socket() failed: errno=0
No error

---------------------------

The test program is small and simple, so i assume the problem is
libpq. But where is the error? I used the plain postgresql-7.1.3
sources and the following Makefile (i copied
./src/include/config.h.win32 to ./src/include/config.h manually):

---------------------------
# Makefile for compiling a native win32 libpq with GNU tools (GNU make)
# =====================================================================
#
# To build an optimized non-debug library, simply compile with
# $ make -f mingw32.mak
#
# To cross-compile from FreeBSD/Linux/..., compile with
# $ make -f mingw32.mak PREFIX=i386-mingw32msvc-
#
# To build the library with debugging support, compile with
# $ make -f mingw32.mak OPT=-g LDOPT=
#
# To build the library with multibyte support, compile with
# $ make -f mingw32.mak MULTIBYTE=1
#
# You can combine any of the above.

#####################################################################
# You can change these parameters on the commandline:
#####################################################################
PREFIX=
OPT=-O3
MULTIBYTE=0
LDOPT=-s
#####################################################################


DEFINES=-DFRONTEND -DHAVE_VSNPRINTF -DHAVE_STRDUP
ifeq (1,$(MULTIBYTE))
DEFINES := $(DEFINES) -DMULTIBYTE=1
endif

CFLAGS=-mno-cygwin -Wall $(OPT) $(DEFINES)
CC=$(PREFIX)gcc
DLLTOOL=$(PREFIX)dlltool
DLLWRAP=$(PREFIX)dllwrap

INC=-I../../../src/include/
LIBS=-lwsock32

#objects=fe-auth.o fe-connect.o fe-exec.o fe-lobj.o fe-misc.o fe-print.o libpqdll.o pqexpbuffer.o pqsignal.o
../../backend/lib/dllist.o../backend/libpq/md5.o 
objects=fe-auth.o fe-connect.o fe-exec.o fe-lobj.o fe-misc.o fe-print.o libpqdll.o pqexpbuffer.o pqsignal.o
../../backend/lib/dllist.o
ifeq (1,$(MULTIBYTE))
objects := $(objects) ../../backend/utils/mb/wchar.o
endif

# Default target
ALL: libpq.dll libpqdll.a libpq.a

$(objects): %.o: %.c
    $(CC) -c $(CFLAGS) $(INC) $< -o $@

# Building the DLL.
libpq.dll: $(objects) libpqdll.def
    $(DLLWRAP) -mno-cygwin --dllname libpq.dll --driver-name $(CC) --def libpqdll.def -o libpq.dll $(objects) $(LDOPT)
--entry_DllMain@12 --target=i386-mingw32 $(LIBS) 

# The import library for the DLL.
libpqdll.a: libpq.dll
    $(DLLTOOL) --def libpqdll.def --dllname libpq.dll --output-lib libpqdll.a

# A static library.
libpq.a: $(objects)
    ar -r libpq.a $(objects)

clean:
    rm -f $(objects) *.dll *.a *~

---------------------------

I'm not very experienced with windows-developing and i'm absolutly
clueless, so any hints and tipps are very welcome.

--
Until the next mail...,
Stefan.

pgsql-cygwin by date:

Previous
From: "Tim Finch, FosterFinch Ltd"
Date:
Subject: Re: PostgreSQL and Cygwin as production database
Next
From: "Andrzej Zeja"
Date:
Subject: Sort odrder in PostgreSql on Cygwin