首页 > 解决方案 > PostgreSQL C API - libq 未定义参考

问题描述

我正在从这里尝试 testPostgreSqlAPI.cpp 示例: http ://www.yolinux.com/TUTORIALS/PostgreSQL-Programming-C-API.html#INSERT_SELECT

我已经在 PostgreSQL 中创建了数据库,我可以看到它是成功的:

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)

但是,当我尝试连接然后使用 testPostgreSqlAPI.cpp 文件从数据库中插入和选择时:

#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;
}

我收到以下错误:

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

如果有人能告诉我为什么会这样,将不胜感激。

谢谢

在此处输入图像描述

标签: cpostgresqlgnat-gps

解决方案


您忘记添加告诉ld链接的链接器选项libpq

-L /path/to/postgresql/lib -l pq

推荐阅读