Thread: Unable to link to libpq using gcc 2.95.3-5
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
/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;
#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
**/
* 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);
}
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int main()
{
char *pghost, *pgport, *pgoptions, *pgtty;
char *dbName;
int nFields;
int i, j;
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
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);
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);
}
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 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))");
"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);
{
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";
*/
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);
}
"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);
"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);
"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;
}
Luis, On Sun, Feb 24, 2002 at 06:03:27PM -0500, Luis R. Alonso wrote: > I compile with the following command: > > g++ -I/usr/include/postgresql -L/lib -lpq Where are the *.o or *.cc files in the command above? > I get output that looks like: > > /temp/ccLZlm8Y.o(.text+0xe):simple.cxx: undefined reference to `PQfinish' > [snip] My WAG is that you are listing the libraries (i.e., -lpq) before the object files. Note that on Linux this will work -- under Cygwin it won't. Jason