当前位置: 技术问答>linux和unix
postgresql入门级问题:编译连接可通过,执行不了,郁闷!!!
来源: 互联网 发布时间:2015-09-25
本文导语: 代码: /* * dbtest.c */ #include #include void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } main() { char *pghost, *pgport, *pgoptions, ...
代码:
/*
* dbtest.c
*/
#include
#include
void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
main()
{
char *pghost,
*pgport,
*pgoptions,
*pgtty;
char *dbName;
int nFields;
int i,
j;
/* FILE *debug; */
PGconn *conn;
PGresult *res;
/*
* begin, by setting the parameters for a backend connection if the
* parameters are null, then the system will try to use reasonable
* defaults by looking up environment variables or, failing that,
* using hardwired constants
*/
pghost = NULL; /* host name of the backend server */
pgport = NULL; /* port of the backend server */
pgoptions = NULL; /* special options to start up the backend
* server */
pgtty = NULL; /* debugging tty for the backend server */
dbName = "template1";
/* make a connection to the database */
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "Connection to database '%s' failed.n", dbName);
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
else
printf("okn");
}
然后编译、连接均可通过:
代码:
gcc -I/usr/local/pgsql/include/ -c dbtest.c
gcc dbtest.o -o dbtest -L/usr/local/pgsql/lib -lpq
当我执行./dbtest的时候,出现如下错误:
error while loading shared libraries: libpq.so.3: cannot open shared object file: No such file or directory
如果我把usr/local/pgsql/lib 下的libpq.so.3复制到dbtest.c所在目录,一切正常,程序正确执行,请问为什么?
/*
* dbtest.c
*/
#include
#include
void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
main()
{
char *pghost,
*pgport,
*pgoptions,
*pgtty;
char *dbName;
int nFields;
int i,
j;
/* FILE *debug; */
PGconn *conn;
PGresult *res;
/*
* begin, by setting the parameters for a backend connection if the
* parameters are null, then the system will try to use reasonable
* defaults by looking up environment variables or, failing that,
* using hardwired constants
*/
pghost = NULL; /* host name of the backend server */
pgport = NULL; /* port of the backend server */
pgoptions = NULL; /* special options to start up the backend
* server */
pgtty = NULL; /* debugging tty for the backend server */
dbName = "template1";
/* make a connection to the database */
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "Connection to database '%s' failed.n", dbName);
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
else
printf("okn");
}
然后编译、连接均可通过:
代码:
gcc -I/usr/local/pgsql/include/ -c dbtest.c
gcc dbtest.o -o dbtest -L/usr/local/pgsql/lib -lpq
当我执行./dbtest的时候,出现如下错误:
error while loading shared libraries: libpq.so.3: cannot open shared object file: No such file or directory
如果我把usr/local/pgsql/lib 下的libpq.so.3复制到dbtest.c所在目录,一切正常,程序正确执行,请问为什么?
|
需要把libpq.so.3 copy到/lib下面,或者将/usr/local/pgsql/lib添加到环境变量中
|
在/etc/ld.so.conf 中加入/usr/local/pgsql/lib
执行ldconfig,就可以了
执行ldconfig,就可以了
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。