|
- /*
- * dbtest.c
- */
- #include <stdio.h>
- #include <libpq-fe.h>
- 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("ok\n");
- }
复制代码
然后编译、连接均可通过:
- 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所在目录,一切正常,程序正确执行,请问为什么? |
|