对 `PQconnectdb' 的未定义引用

undefined reference to `PQconnectdb'

最近我正在做的一个项目需要添加 postgresql 支持。项目是用C写的(开源Janus gateway)。

我按照这些步骤安装了 postgress 和 libps-dev。

sudo apt-get install postgresql
sudo apt-get install libpq-dev
$ sudo -u postgres psql postgres
psql (9.3.9)
Type "help" for help.

postgres=# \password postgres

然后在我的 header(connect.h) 文件中:

#include <postgresql/libpq-fe.h>
#include "debug.h"

/*! \brief Connect to postgresql database
 * If successfull, returns PGconn type
 * If not exits the program
 * @param[in] postgres connection params, ex. username, db, host, etc.
 */
PGconn *connect_to_database(gchar connection_params);

/*! \brief Closes connection to database
 * @param[in] PGconn instance
 */
PGconn *close_connection_to_database(PGconn *pgconn);

还有我的 .c(connect.c) 文件:

/*! \file    db.c
 * \author   ...
 * \copyright GNU General Public License v3
 * \brief    Event handler notifications (headers)
 * \details  This file contains methods for safely creating databse connection
 * to postgresql and then closing it.
 * 
 * \ingroup db
 * \ref db
 */

#include "./connect.h"

PGconn *connect_to_database(gchar connection_params) {
  JANUS_LOG(LOG_WARN, "TESTINGSSS \n\n\n\n");


  const char test_connect = "testings";

  PGconn *conn = PQconnectdb(test_connect);

  return conn;
}

make 我得到:

todns@todnnns-VirtualBox ~/Projects/janus-gateway $ make
make  all-recursive
make[1]: Entering directory '/home/todns/Projects/janus-gateway'
Making all in html
make[2]: Entering directory '/home/todns/Projects/janus-gateway/html'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/home/todns/Projects/janus-gateway/html'
make[2]: Entering directory '/home/todns/Projects/janus-gateway'
  CCLD     janus
db/janus-connect.o: In function `connect_to_database':
/home/todns/Projects/janus-gateway/db/connect.c:20: undefined reference to `PQconnectdb'
collect2: error: ld returned 1 exit status
Makefile:1195: recipe for target 'janus' failed
make[2]: *** [janus] Error 1
make[2]: Leaving directory '/home/todns/Projects/janus-gateway'
Makefile:1914: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/home/todns/Projects/janus-gateway'
Makefile:905: recipe for target 'all' failed
make: *** [all] Error 2

问题是:

db/janus-connect.o: In function `connect_to_database':
    /home/todns/Projects/janus-gateway/db/connect.c:20: undefined

我的直觉告诉我,它与 Makefile.am 文件有关,但我真的不知道要在那里更改什么。 所以问题是:我在此设置中缺少什么?我如何使用我的 C 代码 link postgres-dev 库?

谢谢!

编辑:忘了说了,我是运行这个在LinuxMint虚拟机上(主机是macbook pro)。

想通了。

已将 -L/.... -psql 添加到 AM_CFLAGS Makefile.am

这是完整的行:

AM_CFLAGS += -fstack-protector-all -g -ggdb -fPIC -rdynamic -pthread -L/usr/include/postgresql/libpq -lpq

您可以查看 janus-gateway Makefile.am 看看我的 Makefile.am 没有 -psql

的样子