Обсуждение: libpq Newbie question
Hi, I'm a newbie in postgresql, would like to know how to access database
using libpq. I found example code on postgresql's site, however i'm not able
to execute the code it always giving me an error.
I hope anyone could explain to me how to fix this.
Thank you.
Error:
Undefined first referenced
symbol in file
PQresultStatus /var/tmp/ccED8fcw.o
PQclear /var/tmp/ccED8fcw.o
PQexec /var/tmp/ccED8fcw.o
PQerrorMessage /var/tmp/ccED8fcw.o
PQstatus /var/tmp/ccED8fcw.o
PQconsumeInput /var/tmp/ccED8fcw.o
PQnotifies /var/tmp/ccED8fcw.o
PQfinish /var/tmp/ccED8fcw.o
PQsetdbLogin /var/tmp/ccED8fcw.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
The code:
/*
* testlibpq2.c
* Test of the asynchronous notification interface
*
* Start this program, then from psql in another window do
* NOTIFY TBL2;
*
* Or, if you want to get fancy, try this:
* Populate a database with the following:
*
* CREATE TABLE TBL1 (i int4);
*
* CREATE TABLE TBL2 (i int4);
*
* CREATE RULE r1 AS ON INSERT TO TBL1 DO
* (INSERT INTO TBL2 values (new.i); NOTIFY TBL2);
*
* and do
*
* INSERT INTO TBL1 values (10);
*
*/
#include <stdio.h>
#include "libpq-fe.h"
void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
main()
{
char *pghost,
*pgport,
*pgoptions,
*pgtty;
char *dbName;
int nFields;
int i,
j;
PGconn *conn;
PGresult *res;
PGnotify *notify;
/*
* begin, by setting the parameters for a backend connection if the
* parameters are null, then the system will try to use reasonable
* defaults by looking up environment variables or, failing that,
* using hardwired constants
*/
pghost = NULL; /* host name of the backend server */
pgport = NULL; /* port of the backend server */
pgoptions = NULL; /* special options to start up the backend
* server */
pgtty = NULL; /* debugging tty for the backend server */
dbName = getenv("USER"); /* change this to the name of your test
* database */
/* make a connection to the database */
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
/*
* check to see that the backend connection was successfully made
*/
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
res = PQexec(conn, "LISTEN TBL2");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "LISTEN command failed\n");
PQclear(res);
exit_nicely(conn);
}
/*
* should PQclear PGresult whenever it is no longer needed to avoid
* memory leaks
*/
PQclear(res);
while (1)
{
/*
* wait a little bit between checks; waiting with select()
* would be more efficient.
*/
sleep(1);
/* collect any asynchronous backend messages */
PQconsumeInput(conn);
/* check for asynchronous notify messages */
while ((notify = PQnotifies(conn)) != NULL)
{
fprintf(stderr,
"ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
notify->relname, notify->be_pid);
free(notify);
}
}
/* close the connection to the database and cleanup */
PQfinish(conn);
return 0;
}
On Thu, 29 Nov 2001 22:34:32 -0800, Ronggolawe wrote: > Error: > Undefined first referenced > symbol in file > PQresultStatus /var/tmp/ccED8fcw.o PQclear > /var/tmp/ccED8fcw.o PQexec > /var/tmp/ccED8fcw.o PQerrorMessage > /var/tmp/ccED8fcw.o PQstatus > /var/tmp/ccED8fcw.o PQconsumeInput > /var/tmp/ccED8fcw.o PQnotifies > /var/tmp/ccED8fcw.o PQfinish > /var/tmp/ccED8fcw.o PQsetdbLogin > /var/tmp/ccED8fcw.o ld: fatal: Symbol referencing errors. No output > written to a.out collect2: ld returned 1 exit status > > > I assume you doing: cc -o output file.c instead of: cc -o output file.c -I/opt/local/include -L/opt/local/lib -lpq Note the compiler switch, I think that error was produce because the compiler can't find libpq.
This is a linker error. You need to tell the linker "where" to find the
libpq library.
Add -L/usr/local/pgsql or your equivalent to your compiler call in your make
file.
Tim
----- Original Message -----
From: "Ronggolawe" <ngelawak@www2.us.postgresql.org>
To: <pgsql-general@postgresql.org.pgsql-interfaces@postgresql.org>
Sent: Thursday, November 29, 2001 10:34 PM
Subject: [GENERAL] libpq Newbie question
> Hi, I'm a newbie in postgresql, would like to know how to access database
> using libpq. I found example code on postgresql's site, however i'm not
able
> to execute the code it always giving me an error.
> I hope anyone could explain to me how to fix this.
> Thank you.
>
> Error:
> Undefined first referenced
> symbol in file
> PQresultStatus /var/tmp/ccED8fcw.o
> PQclear /var/tmp/ccED8fcw.o
> PQexec /var/tmp/ccED8fcw.o
> PQerrorMessage /var/tmp/ccED8fcw.o
> PQstatus /var/tmp/ccED8fcw.o
> PQconsumeInput /var/tmp/ccED8fcw.o
> PQnotifies /var/tmp/ccED8fcw.o
> PQfinish /var/tmp/ccED8fcw.o
> PQsetdbLogin /var/tmp/ccED8fcw.o
> ld: fatal: Symbol referencing errors. No output written to a.out
> collect2: ld returned 1 exit status
>
> The code:
> /*
> * testlibpq2.c
> * Test of the asynchronous notification interface
> *
> * Start this program, then from psql in another window do
> * NOTIFY TBL2;
> *
> * Or, if you want to get fancy, try this:
> * Populate a database with the following:
> *
> * CREATE TABLE TBL1 (i int4);
> *
> * CREATE TABLE TBL2 (i int4);
> *
> * CREATE RULE r1 AS ON INSERT TO TBL1 DO
> * (INSERT INTO TBL2 values (new.i); NOTIFY TBL2);
> *
> * and do
> *
> * INSERT INTO TBL1 values (10);
> *
> */
> #include <stdio.h>
> #include "libpq-fe.h"
> void
> exit_nicely(PGconn *conn)
> {
> PQfinish(conn);
> exit(1);
> }
> main()
> {
> char *pghost,
> *pgport,
> *pgoptions,
> *pgtty;
> char *dbName;
> int nFields;
> int i,
> j;
> PGconn *conn;
> PGresult *res;
> PGnotify *notify;
> /*
> * begin, by setting the parameters for a backend connection if the
> * parameters are null, then the system will try to use reasonable
> * defaults by looking up environment variables or, failing that,
> * using hardwired constants
> */
> pghost = NULL; /* host name of the backend server */
> pgport = NULL; /* port of the backend server */
> pgoptions = NULL; /* special options to start up the backend
> * server */
> pgtty = NULL; /* debugging tty for the backend server */
> dbName = getenv("USER"); /* change this to the name of your test
> * database */
> /* make a connection to the database */
> conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
> /*
> * check to see that the backend connection was successfully made
> */
> if (PQstatus(conn) == CONNECTION_BAD)
> {
> fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
> fprintf(stderr, "%s", PQerrorMessage(conn));
> exit_nicely(conn);
> }
> res = PQexec(conn, "LISTEN TBL2");
> if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
> {
> fprintf(stderr, "LISTEN command failed\n");
> PQclear(res);
> exit_nicely(conn);
> }
> /*
> * should PQclear PGresult whenever it is no longer needed to avoid
> * memory leaks
> */
> PQclear(res);
> while (1)
> {
> /*
> * wait a little bit between checks; waiting with select()
> * would be more efficient.
> */
> sleep(1);
> /* collect any asynchronous backend messages */
> PQconsumeInput(conn);
> /* check for asynchronous notify messages */
> while ((notify = PQnotifies(conn)) != NULL)
> {
> fprintf(stderr,
> "ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
> notify->relname, notify->be_pid);
> free(notify);
> }
> }
> /* close the connection to the database and cleanup */
> PQfinish(conn);
> return 0;
> }
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>
>
Oops, fat fingered that one :-(
Make that -L/usr/local/pgsql/lib -lpq
Tim
----- Original Message -----
From: "Ronggolawe" <ngelawak@www2.us.postgresql.org>
To: <pgsql-general@postgresql.org.pgsql-interfaces@postgresql.org>
Sent: Thursday, November 29, 2001 10:34 PM
Subject: [GENERAL] libpq Newbie question
> Hi, I'm a newbie in postgresql, would like to know how to access database
> using libpq. I found example code on postgresql's site, however i'm not
able
> to execute the code it always giving me an error.
> I hope anyone could explain to me how to fix this.
> Thank you.
>
> Error:
> Undefined first referenced
> symbol in file
> PQresultStatus /var/tmp/ccED8fcw.o
> PQclear /var/tmp/ccED8fcw.o
> PQexec /var/tmp/ccED8fcw.o
> PQerrorMessage /var/tmp/ccED8fcw.o
> PQstatus /var/tmp/ccED8fcw.o
> PQconsumeInput /var/tmp/ccED8fcw.o
> PQnotifies /var/tmp/ccED8fcw.o
> PQfinish /var/tmp/ccED8fcw.o
> PQsetdbLogin /var/tmp/ccED8fcw.o
> ld: fatal: Symbol referencing errors. No output written to a.out
> collect2: ld returned 1 exit status
>
> The code:
> /*
> * testlibpq2.c
> * Test of the asynchronous notification interface
> *
> * Start this program, then from psql in another window do
> * NOTIFY TBL2;
> *
> * Or, if you want to get fancy, try this:
> * Populate a database with the following:
> *
> * CREATE TABLE TBL1 (i int4);
> *
> * CREATE TABLE TBL2 (i int4);
> *
> * CREATE RULE r1 AS ON INSERT TO TBL1 DO
> * (INSERT INTO TBL2 values (new.i); NOTIFY TBL2);
> *
> * and do
> *
> * INSERT INTO TBL1 values (10);
> *
> */
> #include <stdio.h>
> #include "libpq-fe.h"
> void
> exit_nicely(PGconn *conn)
> {
> PQfinish(conn);
> exit(1);
> }
> main()
> {
> char *pghost,
> *pgport,
> *pgoptions,
> *pgtty;
> char *dbName;
> int nFields;
> int i,
> j;
> PGconn *conn;
> PGresult *res;
> PGnotify *notify;
> /*
> * begin, by setting the parameters for a backend connection if the
> * parameters are null, then the system will try to use reasonable
> * defaults by looking up environment variables or, failing that,
> * using hardwired constants
> */
> pghost = NULL; /* host name of the backend server */
> pgport = NULL; /* port of the backend server */
> pgoptions = NULL; /* special options to start up the backend
> * server */
> pgtty = NULL; /* debugging tty for the backend server */
> dbName = getenv("USER"); /* change this to the name of your test
> * database */
> /* make a connection to the database */
> conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
> /*
> * check to see that the backend connection was successfully made
> */
> if (PQstatus(conn) == CONNECTION_BAD)
> {
> fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
> fprintf(stderr, "%s", PQerrorMessage(conn));
> exit_nicely(conn);
> }
> res = PQexec(conn, "LISTEN TBL2");
> if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
> {
> fprintf(stderr, "LISTEN command failed\n");
> PQclear(res);
> exit_nicely(conn);
> }
> /*
> * should PQclear PGresult whenever it is no longer needed to avoid
> * memory leaks
> */
> PQclear(res);
> while (1)
> {
> /*
> * wait a little bit between checks; waiting with select()
> * would be more efficient.
> */
> sleep(1);
> /* collect any asynchronous backend messages */
> PQconsumeInput(conn);
> /* check for asynchronous notify messages */
> while ((notify = PQnotifies(conn)) != NULL)
> {
> fprintf(stderr,
> "ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
> notify->relname, notify->be_pid);
> free(notify);
> }
> }
> /* close the connection to the database and cleanup */
> PQfinish(conn);
> return 0;
> }
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>
>