Unable to link to libpq using gcc 2.95.3-5

From: "Luis R(dot) Alonso" <alonso(at)alumni(dot)carnegiemellon(dot)edu>
To: <pgsql-cygwin(at)postgresql(dot)org>
Subject: Unable to link to libpq using gcc 2.95.3-5
Date: 2002-02-24 23:03:27
Message-ID: LAW2-OE76bP1SGnJzXl00015fc3@hotmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-cygwin

Hello,

I have been trying to write simple C++/C programs to interact with the Postgresql database on my Windows 2000 server box, but have been unable to get past the linking stage. I installed Postgresql and gcc using the Cygwin installation utility. The Postgresql version is 7.1.3-2 and the GCC version is 2.95.3-5.

The program I am currently writing is a very simple program that connects to the database, creates a table and inserts a few rows worth of data. It is mostly based on one of the sample programs provided in the documentation. When I compile with the following command:

g++ -I/usr/include/postgresql -L/lib -lpq

I get output that looks like:

/temp/ccLZlm8Y.o(.text+0xe):simple.cxx: undefined reference to `PQfinish'
/temp/ccLZlm8Y.o(.text+0x392):simple.cxx: undefined reference to `PQsetdbLogin'
/temp/ccLZlm8Y.o(.text+0x3a6):simple.cxx: undefined reference to `PQstatus'
collect2: ld returned 1 exit status

There is a line for each of the PQ* functions that I call in my code.

libpq.a is in /lib and libpq-fe.h is in /usr/include/postgresql. The database itself works fine, I can connect to it using the tools provided and can manipulate data.

Can anyone give me an idea as to what may be wrong? I've been looking through mailing list archives and have been unable to find anything useful. My code is below.

Thanks,

Luis

-------------- code snippet -------------------------------

#include <iostream.h>
#include "libpq-fe.h"
using namespace std;

/*
* This function lets us close things up nicely so we don't have
* to worry about being a bunch of idiots
**/

static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}

int main()
{

char *pghost, *pgport, *pgoptions, *pgtty;
char *dbName;
int nFields;
int i, j;

PGconn *conn;
PGresult *res;

//set up the parameters for the connection
pghost = NULL; // we don't have a host, so it don't matter
pgport = NULL; //again, we don't have a host, so the default will work
pgoptions = NULL; //Don't really care much for extra options right now
pgtty = NULL; //don't have a TTY to send data to
dbName = "learning"; //for now hard code it, will change later

//make a connection to the database
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);

//check connection for a success
if (PQstatus(conn) == CONNECTION_BAD)
{
cout << "Connection to database " << dbName << " failed\n";
cout << PQerrorMessage(conn);
exit_nicely(conn);
}

cout << "\n\tConnection made, tracing\n";

//ok, going to attempt to keep it nice and simple and just create my tables

cout << "\nAttempting to create jobs table";
res = PQexec(conn,
"create table jobs (jobid int primary key, name varchar(80))");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
cout << "Create jobs table command failed";
PQclear(res);
exit_nicely(conn);
}
else
{
cout << "\nCreate job table passed";
}
PQclear(res);

res = PQexec(conn,
"create table person (personid int primary key, \
firstName varchar(80), lastName varchar(80), job int, \
constraint fork foreign key(job) references jobs(jobid))");

if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
cout << "Create person table command failed";
PQclear(res);
exit_nicely(conn);
}
else
{
cout << "\nCreate person table passes";
}
PQclear(res);

/*
char* jobs = "Software Developer", "Software Engineer", "Senior Engineer",
"Manager", "Chief Technology Officer",
"Chief Executive Officer";
*/

res = PQexec(conn,
"insert into jobs values(0, 'Software Developer')");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
cout << "Insert failed for " << i;
PQclear(res);
exit_nicely(conn);
}

res = PQexec(conn,
"insert into jobs values(1, 'Software Engineer')");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
cout << "Insert failed for " << i;
PQclear(res);
exit_nicely(conn);
}
PQclear(res);

res = PQexec(conn,
"insert into jobs values(2, 'Senior Engineer')");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
cout << "Insert failed for " << i;
PQclear(res);
exit_nicely(conn);
}
PQclear(res);

cout << "\nClosing the connection to the database";

PQfinish(conn);

return 0;

}

Responses

Browse pgsql-cygwin by date

  From Date Subject
Next Message Jason Tishler 2002-02-25 02:52:45 Re: no cygrunsrv with cygwin
Previous Message Dirk Harms 2002-02-23 19:53:11 Problem to start ´postmaster´ on win95 (cygwin)