0

I am attempting the testPostgreSqlAPI.cpp example from here: http://www.yolinux.com/TUTORIALS/PostgreSQL-Programming-C-API.html#INSERT_SELECT

I have created the database in PostgreSQL which I can see is successful:

bedrock=# SELECT * FROM employee;
 idpk |    employee_name     |         dept         |       jobtitle       
------+----------------------+----------------------+----------------------
    1 | Fred Flinstone       | Worker               | Rock Digger         
    2 | Wilma Flinstone      | Finance              | Analyst             
    3 | Barney Rubble        | Sales                | Neighbor            
(3 rows)

bedrock=# \d employee
                                    Table "public.employee"
    Column     |     Type      | Collation | Nullable |                Default                 
---------------+---------------+-----------+----------+----------------------------------------
 idpk          | integer       |           | not null | nextval('employee_idpk_seq'::regclass)
 employee_name | character(20) |           |          | 
 dept          | character(20) |           |          | 
 jobtitle      | character(20) |           |          | 
Indexes:
    "employee_pkey" PRIMARY KEY, btree (idpk)

However, when I attempt to connect and then insert and selct from the database using the testPostgreSqlAPI.cpp file:

#include <stdio.h>
#include <stdlib.h>
#include </usr/include/postgresql/libpq-fe.h>



/* 
 * Connect to an existing database.
 * Insert a new record for Betty.
 * Select full contents of the table and print all fields.
 */

static void
exit_nicely(PGconn *conn, PGresult   *res)
{
    PQclear(res);
    PQfinish(conn);
    exit(1);
}

int
main(int argc, char **argv)
{
    const char *conninfo = "dbname=bedrock sslmode=disable";
    PGconn     *conn;
    PGresult   *res;
    int         nFields;
    int         i,
                j;

    // 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));
        PQfinish(conn);
        exit(1);
    }

    res = PQexec(conn, "INSERT INTO employee (Employee_Name,Dept,JobTitle) VALUES ('Betty Rubble','IT','Neighbor')");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "INSERT failed: %s", PQerrorMessage(conn));
        exit_nicely(conn,res);
    }
    PQclear(res);

    // Use cursor inside a transaction block

    // Start a transaction block
    res = PQexec(conn, "BEGIN");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
        exit_nicely(conn,res);
    }
    PQclear(res);  // Clear memory

    res = PQexec(conn, "DECLARE mydata CURSOR FOR select * from employee");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
        exit_nicely(conn,res);
    }
    PQclear(res);

    res = PQexec(conn, "FETCH ALL in mydata");
    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
        exit_nicely(conn,res);
    }

    // first, print out the table column attribute names
    nFields = PQnfields(res);
    for (i = 0; i < nFields; i++)
        printf("%-15s", PQfname(res, i));
    printf("\n\n");

    // next, print out the rows of data
    for (i = 0; i < PQntuples(res); i++)
    {
        for (j = 0; j < nFields; j++)
            printf("%-15s", PQgetvalue(res, i, j));
        printf("\n");
    }

    PQclear(res);

    // close the portal ... we don't bother to check for errors ...
    res = PQexec(conn, "CLOSE mydata");
    PQclear(res);

    // End the transaction 
    res = PQexec(conn, "END");
    PQclear(res);

    // close the connection to the database and cleanup 
    PQfinish(conn);

    return 0;
}

I recieve the following error:

gprbuild -d -P/home/parallels/Documents/C++ Projects/Connecting to a DB/default.gpr /home/parallels/Documents/C++ Projects/Connecting to a DB/src/main.cpp
Link
   [link]         main.cpp
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.o: in function `exit_nicely(pg_conn*, pg_result*)':
main.cpp:(.text+0x18): undefined reference to `PQclear'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x24): undefined reference to `PQfinish'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.o: in function `main':
main.cpp:(.text+0x51): undefined reference to `PQconnectdb'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x61): undefined reference to `PQstatus'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x76): undefined reference to `PQerrorMessage'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x9e): undefined reference to `PQfinish'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0xb9): undefined reference to `PQexec'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0xc9): undefined reference to `PQresultStatus'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0xdf): undefined reference to `PQerrorMessage'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x11a): undefined reference to `PQclear'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x12b): undefined reference to `PQexec'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x13b): undefined reference to `PQresultStatus'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x151): undefined reference to `PQerrorMessage'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x18c): undefined reference to `PQclear'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x19d): undefined reference to `PQexec'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x1ad): undefined reference to `PQresultStatus'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x1c3): undefined reference to `PQerrorMessage'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x1fe): undefined reference to `PQclear'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x20f): undefined reference to `PQexec'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x21f): undefined reference to `PQresultStatus'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x235): undefined reference to `PQerrorMessage'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x270): undefined reference to `PQnfields'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x293): undefined reference to `PQfname'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x2c8): undefined reference to `PQntuples'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x2f5): undefined reference to `PQgetvalue'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x329): undefined reference to `PQclear'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x33a): undefined reference to `PQexec'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x34a): undefined reference to `PQclear'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x35b): undefined reference to `PQexec'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x36b): undefined reference to `PQclear'
/home/parallels/opt/GNAT/2020/bin/../libexec/gcc/x86_64-pc-linux-gnu/9.3.1/ld: main.cpp:(.text+0x377): undefined reference to `PQfinish'
collect2: error: ld returned 1 exit status
gprbuild: link of main.cpp failed
gprbuild: failed command was: /home/parallels/opt/GNAT/2020/bin/g++ main.o libdefault.a -shared-libgcc -o main
[2020-07-08 10:13:10] process exited with status 4, elapsed time: 01.21s

If anyone would be able to tell me why this is, it would greatly appreciated.

Thanks

enter image description here

1 Answer 1

1

You forgot to add linker options that tell ld to link with libpq:

-L /path/to/postgresql/lib -l pq
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Laurenz - Please can you confirm if I need to add the linker to the testPostgreSqlAPI.cpp file (main.cpp) in my case, or the default.gpr file? I have included a screen shot to the OP for reference. Cheers
This is not a problem of the source code, but of the build process. So something that configures gprbuild.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.