LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Error in compiling Postgres FE C program (https://www.linuxquestions.org/questions/programming-9/error-in-compiling-postgres-fe-c-program-538076/)

skie_knite007 03-16-2007 01:33 PM

Error in compiling Postgres FE C program
 
Hi folks,

I am a newbie to postgres FE programming. I am trying to connect to Postgres db like

Code:

#include <stdio.h>
#include "pgsql/libpq-fe.h"

int
main(int argc, char **argv)
{
  const char *conninfo;
        PGconn    *conn;
        PGresult  *res;
        extern PGconn *PQconnectStart(const char *conninfo);
          if (argc > 1)
                conninfo = argv[1];
        else
                conninfo = "dbname = table";

        /* Make a connection to the database */
        conn = PQconnectdb(conninfo);

        /* Check to see that the backend connection was successfully made */
        if (PQstatus(conn) != CONNECTION_OK)
        {
                fprintf(stderr, "Connection to database failed: %s",
                        PQerrorMessage(conn));
                exit_nicely(conn);
        }

I compiled it just like ordinary C pgms, using
gcc <My program.c>


All the functions like PQconnectdb() are defined in libpq-fe.h

Now my compiler is throwing error like

"
undefined reference to `PQconnectdb'

"


But this is defined in the header file.... Again I tried copying the entire header file in my program. ( All the defenitions are now in the program itself)

Again it is showing the undefined reference problem..............
What could be wrong????
please help me

Do I need to add any extra options while compiling?

osor 03-16-2007 01:48 PM

You need to link to the postgre libraries. Try passing the line -lpq to gcc (i.e., “gcc -lpq <My program.c>”).

Btw, your compiler is not really throwing the error, it’s the linker.

skie_knite007 03-16-2007 10:38 PM

Thanks a lot osor. It did help..


All times are GMT -5. The time now is 04:00 AM.