无法编译测试PostgreSQL程序

时间:2013-10-11 06:46:16

标签: c++ postgresql libpqxx

当我用C ++编译我的PostgreSQL程序时,我得到了这些错误。谁能告诉我原因呢。

user@linux-yyzo:~/WorkDir/Program> g++ test.cpp -lpqxx -I/usr/local/include -I/usr/local/pgsql/include

/usr/local/lib/libpqxx.a(util.o): In function `pqxx::describe_thread_safety()':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/util.cxx:69: undefined reference to `PQisthreadsafe'
/usr/local/lib/libpqxx.a(util.o): In function `pqxx::internal::freepqmem(void const*)':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/util.cxx:195: undefined reference to `PQfreemem'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::internal::result_data::~result_data()':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:55: undefined reference to `PQclear'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::size() const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:101: undefined reference to `PQntuples'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::empty() const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:107: undefined reference to `PQntuples'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::CmdStatus() const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:241: undefined reference to `PQcmdStatus'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::inserted_oid() const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:256: undefined reference to `PQoidValue'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::affected_rows() const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:262: undefined reference to `PQcmdTuples'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::GetIsNull(unsigned long, unsigned int) const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:279: undefined reference to `PQgetisnull'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::GetLength(unsigned long, unsigned int) const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:286: undefined reference to `PQgetlength'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::errorposition() const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:344: undefined reference to `PQresultErrorField'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::ThrowSQLError(std::string const&, std::string const&) const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:132: undefined reference to `PQresultErrorField'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::columns() const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:363: undefined reference to `PQnfields'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::column_name(unsigned int) const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:353: undefined reference to `PQfname'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::table_column(unsigned int) const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:318: undefined reference to `PQftablecol'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::StatusError() const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:214: undefined reference to `PQresultStatus'
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:233: undefined reference to `PQresultStatus'
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:228: undefined reference to `PQresultErrorMessage'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::column_table(unsigned int) const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/result.cxx:303: undefined reference to `PQftable'
/usr/local/lib/libpqxx.a(result.o): In function `pqxx::result::column_type(unsigned int) const':

/usr/local/lib/libpqxx.a(connection.o): In function `pqxx::connect_direct::do_startconnect(pg_conn*)':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/connection.cxx:94: undefined reference to `PQstatus'
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/connection.cxx:96: undefined reference to `PQerrorMessage'
/usr/local/lib/libpqxx.a(tuple.o): In function `pqxx::result::column_number(char const*) const':
/home/user/WorkDir/Program/Connections/libpqxx-4.0/src/tuple.cxx:194: undefined reference to `PQfnumber'
collect2: error: ld returned 1 exit status

1 个答案:

答案 0 :(得分:2)

您需要将-lpq作为选项添加到GCC以链接libpq本身。它必须追踪-lpqxx

g++ test.cpp -lpqxx -lpq -I/usr/local/include -I/usr/local/pgsql/include
#                   ^^^^ here

这是必需的,因为libpqxx只是libpq的C接口的C ++包装器,它是PostgreSQL的真正客户端库。如果在系统的库路径中找不到-L/path/to/libpq,则可能需要在-lpq之前添加{{1}}。