1

我正在尝试使用 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

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

/* 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");
}

有没有要包含的库或我错过的任何步骤?

4

1 回答 1

1

您忘记与 ECPG 库链接。

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

cc -o testecpg testecpg.c -lecpg

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

于 2020-03-20T13:14:49.607 回答