PostgreSQL ECPG 数据库连接问题

PostgreSQL ECPG database connection issue

我正在尝试使用 ecpg 程序连接到 PostgreSQL 数据库,但出现以下错误。

cc testecpg.c
/tmp/ccSzqgA7.o: In function `main':
testecpg.c:(.text+0x5d): undefined reference to `ECPGconnect'
testecpg.c:(.text+0x62): undefined reference to `ECPGget_sqlca'
testecpg.c:(.text+0x70): undefined reference to `sqlprint'
collect2: error: ld returned 1 exit status

testecpg.c执行ecpg后生成的文件testecpg.pgc

/* Processed by ecpg (4.11.0) */
/* These include files are added by the preprocessor */
#include "/opt/rh/rh-postgresql95/root/usr/include/ecpglib.h"
#include "/opt/rh/rh-postgresql95/root/usr/include/ecpgerrno.h"
#include "/opt/rh/rh-postgresql95/root/usr/include/sqlca.h"
/* End of automatic include section */

#line 1 "testecpg.pgc"
#include <stdio.h>
#include "/opt/rh/rh-postgresql95/root/usr/include/libpq-fe.h"
int main(void)
{
/* exec sql begin declare section */

#line 6 "testecpg.pgc"
 char * dbname = "dbname" ;

#line 7 "testecpg.pgc"
 char * db = "dbname@hostname:5432" ;

#line 8 "testecpg.pgc"
 char * user = "user" ;

#line 9 "testecpg.pgc"
 char * passwd = "password" ;

#line 10 "testecpg.pgc"
 const char * target = "dbname@hostname:5432" ;
/* exec sql end declare section */
#line 11 "testecpg.pgc"

/* exec sql whenever sqlerror  sqlprint ; */
#line 12 "testecpg.pgc"

{ ECPGconnect(0, 0, target , user , passwd , NULL, 0);
#line 13 "testecpg.pgc"

if (sqlca.sqlcode < 0) sqlprint();}
#line 13 "testecpg.pgc"

printf("connection succssfull");
}

是否有任何库要包含或我错过了任何步骤?

您忘记 link 使用 ECPG 库。

在 Unix 系统上,这看起来有点像

cc -o testecpg testecpg.c -lecpg

您必须添加适当的 -I-L 选项,以便编译器可以找到包含文件和库。