Thread: Using C API

Using C API

From
pgsql.gen@tuxbeagle.com
Date:
I was trying this example program out of a well known postgresql book and keep getting errors whenever I try to
compile.

Error messages ##make client1

cc -g  -I /usr/include -I /usr/include/pgsql -I /usr/include/pgsql/server   -c -o client1.o client1.c
cc -g  client1.o  -L /usr/lib -L /usr/lib/pgsql -o client1
client1.o: In function `main':
/root/PROGRAMMING/C_API/client1.c:10: undefined reference to `PQconnectdb'
/root/PROGRAMMING/C_API/client1.c:11: undefined reference to `PQfinish'
collect2: ld returned 1 exit status
make: *** [client1] Error 1
---------------------------------------------------------

--------------------------------------------------
/*
** File: client1.c
*/

#include "libpq-fe.h"

int main( void )
{
        PGconn * connection;
        connection = PQconnectdb( "dbname='MyTestDB'" );
## I tried the above line with
##      connection = PQconnectdb( "" );
## as per the book
        PQfinish( connection ) ;
        return( 0 );
}
-----------------------------------------------------
## File: Makefile
##
##   Rules to create libpq sample applications
##

CPPFLAGS += -I /usr/include -I /usr/include/pgsql -I /usr/include/pgsql/server
CFLAGS += -g
LDFLAGS += -g
LDLIBS += -L /usr/lib -L /usr/lib/pgsql
client1: client1.o
-------------------------------------------------

following are the rpms I have installed on a RHEL5 system.
postgresql-plperl-8.2.4-1PGDG
postgresql-contrib-8.2.4-1PGDG
postgresql-python-8.1.9-1.el5
postgresql-docs-8.2.4-1PGDG
postgresql-server-8.2.4-1PGDG
postgresql-devel-8.2.4-1PGDG
postgresql-8.2.4-1PGDG
postgresql-plpython-8.2.4-1PGDG
compat-postgresql-libs-4-2PGDG.rhel4
postgresql-libs-8.2.4-1PGDG
postgresql-pltcl-8.2.4-1PGDG

Yes I see the compat one and will install rhel5 if I find one.

Re: Using C API

From
Lars Heidieker
Date:
On 10 Oct 2007, at 02:25, pgsql.gen@tuxbeagle.com wrote:

I was trying this example program out of a well known postgresql book and keep getting errors whenever I try to compile.

Error messages ##make client1

cc -g  -I /usr/include -I /usr/include/pgsql -I /usr/include/pgsql/server   -c -o client1.o client1.c
cc -g  client1.o  -L /usr/lib -L /usr/lib/pgsql -o client1
client1.o: In function `main':
/root/PROGRAMMING/C_API/client1.c:10: undefined reference to `PQconnectdb'
/root/PROGRAMMING/C_API/client1.c:11: undefined reference to `PQfinish'
collect2: ld returned 1 exit status
make: *** [client1] Error 1
---------------------------------------------------------




you must add -lpq too your LDFLAGS
in order to link the lib you only have the linker paths set in LDFLAGS

it should read like this then:
cc -g  client1.o  -L /usr/lib -L /usr/lib/pgsql -o client1 -lpq

--

Viele Grüße,
Lars Heidieker


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

Mystische Erklärungen.
Die mystischen Erklärungen gelten für tief;
die Wahrheit ist, dass sie noch nicht einmal oberflächlich sind.
     -- Friedrich Nietzsche



Attachment

Re: Using C API

From
Tom Lane
Date:
pgsql.gen@tuxbeagle.com writes:
> I was trying this example program out of a well known postgresql book and keep getting errors whenever I try to
compile.

> cc -g  -I /usr/include -I /usr/include/pgsql -I /usr/include/pgsql/server   -c -o client1.o client1.c
> cc -g  client1.o  -L /usr/lib -L /usr/lib/pgsql -o client1
> client1.o: In function `main':
> /root/PROGRAMMING/C_API/client1.c:10: undefined reference to `PQconnectdb'
> /root/PROGRAMMING/C_API/client1.c:11: undefined reference to `PQfinish'
> collect2: ld returned 1 exit status

You're missing "-lpq" in the link step.  I think most or all of those -I
and -L switches are useless, also.

            regards, tom lane

Re: Using C API

From
Greg Smith
Date:
On Tue, 9 Oct 2007, pgsql.gen@tuxbeagle.com wrote:

> following are the rpms I have installed on a RHEL5 system.
>
> compat-postgresql-libs-4-2PGDG.rhel4
>
> Yes I see the compat one and will install rhel5 if I find one.

It has no bearing on what you were running into, and unless you're having
a problem there's little reason to fix this, but the file you want is at
http://www.postgresql.org/ftp/binary/v8.2.4/linux/rpms/redhat/rhel-es-5/

If I recall correctly here, there were a few weeks where the RPMs on the
site for RHEL5 accidentally included the wrong compat library, and I'm
guessing you got your copy during that period.

--
* Greg Smith gsmith@gregsmith.com http://www.gregsmith.com Baltimore, MD