Thread: undefined reference to `PQprepare' with postgresql-dev 7.4.7-6sarge2

undefined reference to `PQprepare' with postgresql-dev 7.4.7-6sarge2

From
"Alexander Farber"
Date:
Hello,

I'm trying to compile a libpq program under Debian 3.1r2 with these
packages installed:

$ dpkg -l | grep postgres
ii  postgresql     7.4.7-6sarge2  object-relational SQL database management sy
ii  postgresql-cli 7.4.7-6sarge2  front-end programs for PostgreSQL
ii  postgresql-con 7.4.7-6sarge2  additional facilities for PostgreSQL
ii  postgresql-dev 7.4.7-6sarge2  development files for libpq (PostgreSQL libr
ii  postgresql-doc 7.4.7-6sarge2  documentation for the PostgreSQL database ma

That program compiles and works fine with OpenBSD
and Cygwin (I'm using PostgreSQL 8.x there though...)

On Debian it unfortunately doesn't link:

$ gcc build/pref.o build/message.o build/pgsql.o build/user.o
build/util.o build/table.o build/common.o build/array.o
build/xstring.o build/strlcpy.o build/strlcat.o build/daemon.o -o pref
-L /usr/lib -L  /usr/lib/postgresql/lib -lpq
build/pgsql.o(.text+0x15b): In function `db_prepare':
server/pgsql.c:57: undefined reference to `PQprepare'
collect2: ld returned 1 exit status

$ pg_config --version
PostgreSQL 7.4.7

$ pg_config --libdir
/usr/lib

$ objdump -x /usr/lib/libpq.a | grep -i PQprepare
00000000         *UND*  00000000 pqPrepareAsyncResult
00001974 R_386_PLT32       pqPrepareAsyncResult
00000490 g     F .text  0000007d pqPrepareAsyncResult
000012e5 R_386_PLT32       pqPrepareAsyncResult
0000130a R_386_PLT32       pqPrepareAsyncResult
00000000         *UND*  00000000 pqPrepareAsyncResult
00001841 R_386_PLT32       pqPrepareAsyncResult

There are very few hits for the "undefined reference PQprepare"
on Google (which I take a good sign :-)

Does anybody have an idea please, what could I be doing wrong?

Why doesn't objdump find PQprepare, but finds PQconnectdb?

$ objdump -x /usr/lib/libpq.a | grep -i PQconnectdb
00000000 g     F .text  00000042 PQconnectdb

Thank you
Alex

PS: And here is the code failing to link (my server/pgsql.c)
      probably nothing special:

#define DB_CONN_STR             "host=/tmp user=XXX dbname=XXX"
#define SQL_FETCH_USER          \
    "select username, user_avatar from phpbb_users where user_active = 1 " \
    "and user_id = $1 and user_password = $2 and user_id not in " \
    "(select ban_userid from phpbb_banlist where ban_userid is not null)"
......
static void
db_reconnect()
{
        PQreset(conn);

        if (PQstatus(conn) != CONNECTION_OK) {
                warn("Connection to db '%s' failed: %s",
                    DB_CONN_STR, PQerrorMessage(conn));
                sleep(RETRY_INTERVAL);
                return;
        }

        db_prepare("sql_fetch_user", SQL_FETCH_USER, 2);
}

void
db_prepare(const char *stname, const char *query, int nparams)
{
        PGresult*       res;

        res = PQprepare(conn, stname, query, nparams, NULL);
        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
                PQclear(res);
                db_disconnect();
                die("Preparing statement '%s' failed: %s",
                     query, PQerrorMessage(conn));
        }

        PQclear(res);
}
......

--
http://preferans.de

Re: undefined reference to `PQprepare' with postgresql-dev

From
Stefan Kaltenbrunner
Date:
Alexander Farber wrote:
> Hello,
>
> I'm trying to compile a libpq program under Debian 3.1r2 with these
> packages installed:
>
> $ dpkg -l | grep postgres
> ii  postgresql     7.4.7-6sarge2  object-relational SQL database
> management sy
> ii  postgresql-cli 7.4.7-6sarge2  front-end programs for PostgreSQL
> ii  postgresql-con 7.4.7-6sarge2  additional facilities for PostgreSQL
> ii  postgresql-dev 7.4.7-6sarge2  development files for libpq
> (PostgreSQL libr
> ii  postgresql-doc 7.4.7-6sarge2  documentation for the PostgreSQL
> database ma
>
> That program compiles and works fine with OpenBSD
> and Cygwin (I'm using PostgreSQL 8.x there though...)
>
> On Debian it unfortunately doesn't link:
>
> $ gcc build/pref.o build/message.o build/pgsql.o build/user.o
> build/util.o build/table.o build/common.o build/array.o
> build/xstring.o build/strlcpy.o build/strlcat.o build/daemon.o -o pref
> -L /usr/lib -L  /usr/lib/postgresql/lib -lpq
> build/pgsql.o(.text+0x15b): In function `db_prepare':
> server/pgsql.c:57: undefined reference to `PQprepare'
> collect2: ld returned 1 exit status
>
> $ pg_config --version
> PostgreSQL 7.4.7
>
> $ pg_config --libdir
> /usr/lib
>
> $ objdump -x /usr/lib/libpq.a | grep -i PQprepare
> 00000000         *UND*  00000000 pqPrepareAsyncResult
> 00001974 R_386_PLT32       pqPrepareAsyncResult
> 00000490 g     F .text  0000007d pqPrepareAsyncResult
> 000012e5 R_386_PLT32       pqPrepareAsyncResult
> 0000130a R_386_PLT32       pqPrepareAsyncResult
> 00000000         *UND*  00000000 pqPrepareAsyncResult
> 00001841 R_386_PLT32       pqPrepareAsyncResult
>
> There are very few hits for the "undefined reference PQprepare"
> on Google (which I take a good sign :-)
>
> Does anybody have an idea please, what could I be doing wrong?


there is no PQprepare in 7.4 - it got introduced in 8.0 (it is not
mentioned in the 7.4 manual either) ...


Stefan

Re: undefined reference to `PQprepare' with postgresql-dev 7.4.7-6sarge2

From
"Alexander Farber"
Date:
I've also checked
http://www.postgresql.org/docs/7.4/interactive/libpq-exec.html
and PQprepare() isn't mentioned there at all.
Wasn't it provided in PostgreSQL version 7?

Is there a way to workaround it?
Maybe by using pqPrepareAsyncResult()?

And how could I #ifdef in my libpq-program, to
detect an older PostgreSQL version? The headers

   /usr/include/postgresql/libpq-fe.h and
   /usr/include/postgresql/postgres_ext.h

don't have any PG_VERSION or similar define...


On 8/24/06, Alexander Farber <alexander.farber@gmail.com> wrote:
> I'm trying to compile a libpq program under Debian 3.1r2 with these
> packages installed:
>
> $ dpkg -l | grep postgres
> ii  postgresql     7.4.7-6sarge2  object-relational SQL database management sy
> ii  postgresql-cli 7.4.7-6sarge2  front-end programs for PostgreSQL
> ii  postgresql-con 7.4.7-6sarge2  additional facilities for PostgreSQL
> ii  postgresql-dev 7.4.7-6sarge2  development files for libpq (PostgreSQL libr
> ii  postgresql-doc 7.4.7-6sarge2  documentation for the PostgreSQL database ma
>
> On Debian it unfortunately doesn't link:
>
> $ gcc build/pref.o build/message.o build/pgsql.o build/user.o
> build/util.o build/table.o build/common.o build/array.o
> build/xstring.o build/strlcpy.o build/strlcat.o build/daemon.o -o pref
> -L /usr/lib -L  /usr/lib/postgresql/lib -lpq
> build/pgsql.o(.text+0x15b): In function `db_prepare':
> server/pgsql.c:57: undefined reference to `PQprepare'
> collect2: ld returned 1 exit status
>
> $ pg_config --version
> PostgreSQL 7.4.7
>
> $ pg_config --libdir
> /usr/lib
>
> $ objdump -x /usr/lib/libpq.a | grep -i PQprepare
> 00000000         *UND*  00000000 pqPrepareAsyncResult
> 00001974 R_386_PLT32       pqPrepareAsyncResult
> 00000490 g     F .text  0000007d pqPrepareAsyncResult
> 000012e5 R_386_PLT32       pqPrepareAsyncResult
> 0000130a R_386_PLT32       pqPrepareAsyncResult
> 00000000         *UND*  00000000 pqPrepareAsyncResult
> 00001841 R_386_PLT32       pqPrepareAsyncResult
>
> There are very few hits for the "undefined reference PQprepare"
> on Google (which I take a good sign :-)
>
> Does anybody have an idea please, what could I be doing wrong?
>
> Why doesn't objdump find PQprepare, but finds PQconnectdb?
>
> $ objdump -x /usr/lib/libpq.a | grep -i PQconnectdb
> 00000000 g     F .text  00000042 PQconnectdb
>
> Thank you
> Alex
>
> PS: And here is the code failing to link (my server/pgsql.c)
>       probably nothing special:
>
> #define DB_CONN_STR             "host=/tmp user=XXX dbname=XXX"
> #define SQL_FETCH_USER          \
>     "select username, user_avatar from phpbb_users where user_active = 1 " \
>     "and user_id = $1 and user_password = $2 and user_id not in " \
>     "(select ban_userid from phpbb_banlist where ban_userid is not null)"
> ......
> static void
> db_reconnect()
> {
>         PQreset(conn);
>
>         if (PQstatus(conn) != CONNECTION_OK) {
>                 warn("Connection to db '%s' failed: %s",
>                     DB_CONN_STR, PQerrorMessage(conn));
>                 sleep(RETRY_INTERVAL);
>                 return;
>         }
>
>         db_prepare("sql_fetch_user", SQL_FETCH_USER, 2);
> }
>
> void
> db_prepare(const char *stname, const char *query, int nparams)
> {
>         PGresult*       res;
>
>         res = PQprepare(conn, stname, query, nparams, NULL);
>         if (PQresultStatus(res) != PGRES_COMMAND_OK) {
>                 PQclear(res);
>                 db_disconnect();
>                 die("Preparing statement '%s' failed: %s",
>                      query, PQerrorMessage(conn));
>         }
>
>         PQclear(res);
> }
> ......

--
http://preferans.de

Re: undefined reference to `PQprepare' with postgresql-dev 7.4.7-6sarge2

From
Tom Lane
Date:
"Alexander Farber" <alexander.farber@gmail.com> writes:
> Does anybody have an idea please, what could I be doing wrong?

Trying to use a subroutine added in 8.0 in 7.4.

            regards, tom lane

Re: undefined reference to `PQprepare' with postgresql-dev 7.4.7-6sarge2

From
"Alexander Farber"
Date:
Yes, I'm sorry I didn't expect that it wasn't there
(coming to libpq-programming from Perl,
where there is always a prepare() function)

How could I detect (#ifdef WHAT)  an older
PostgreSQL version in my C-code?

And is there maybe an easy workaround
for a missing PQprepare?

On 8/24/06, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> "Alexander Farber" <alexander.farber@gmail.com> writes:
> > Does anybody have an idea please, what could I be doing wrong?
>
> Trying to use a subroutine added in 8.0 in 7.4.

Regards
Alex

--
http://preferans.de

Re: undefined reference to `PQprepare' with postgresql-dev 7.4.7-6sarge2

From
"Alexander Farber"
Date:
Ok, I've upgraded to:

   $ dpkg -l | grep postgres
   ii  postgresql-8.1 8.1.4-4bpo1    object-relational SQL database,
version 8.1
   ii  postgresql-cli 8.1.4-4bpo1    front-end programs for PostgreSQL 8.1
   ii  postgresql-cli 57bpo1         manager for multiple PostgreSQL
client versi
   ii  postgresql-com 57bpo1         manager for PostgreSQL database clusters
   ii  postgresql-con 8.1.4-4bpo1    additional facilities for PostgreSQL
   ii  postgresql-doc 8.1.4-4bpo1    documentation for the PostgreSQL
database ma

   $ dpkg -l | grep pq
   ii  libpq-dev      8.1.4-4bpo1    header files for libpq4
(PostgreSQL library)
   ii  libpq4         8.1.4-4bpo1    PostgreSQL C client library

using http://www.backports.org/dokuwiki/doku.php
and now my program compiles and PQprepare is there:

   $ objdump -x /usr/lib/libpq.a | grep -iw PQprepare
   000016a0 g     F .text  00000086 PQprepare

Regards
Alex

--
http://preferans.de