ubuntu14.04下使用libpq++

Postgres的c++接口早就改为libpqxx,libpq++在ubuntu14.04下不能编译通过,为了使用libpq++需要做一些修改

一、下载libpq++-4.0.tar.gz并解压

http://download.chinaunix.net/download/0006000/5978.shtml

二、进入libpq++-4.0目录

三、打开Makefile

修改POSTGRES_HOME=/opt/PostgreSQL/9.3 (你的postgresql安装目录)

修改CXXOPTS=-fPIC -DHAVE_NAMESPACE_STD -DHAVE_CXX_STRING_HEADER -DDLLIMPORT=""

64位系统必需加上-fPIC

四、打开pgdatabase.cc

在最前面包含stdlib.h文件名(否则atoi函数找不到)

五、make&sudo make install

六、/etc/ld.so.conf.d/ 或LD_LIBRARY_PATH加上库搜寻路径

并执行sudo ldconfig -v

或者在编译的时候加上-Wl,-rpath=/opt/PostgreSQL/9.3/lib

七、测试

test.cpp(把mydb,mytable修改一下

  1. #include <stdlib.h>

  2. #include <iostream>

  3. #include <libpq++.h>

  4. using namespace std;

  5. int main() {

  6. char query_string[256]= "SELECT * FROM mytable;";

  7. PgDatabase data("dbname = mydb");

  8. if (data.ConnectionBad()) {

  9. cout <<"connected failed" << endl;

  10. cout <<"Error is "<<data.ErrorMessage() << endl;

  11. exit(1);

  12. }

  13. if (! data.ExecTuplesOk(query_string)) {

  14. cout<<"Query Failed!" << endl;

  15. exit(1);

  16. }

  17. for(int k=0; k

  18. cout<<data.FieldName(k);

  19. cout <<" " ;

  20. }

  21. cout<<endl;

  22. for (int i = 0; i < data.Tuples(); i++) {

  23. for(int k=0; k

  24. cout << data.GetValue(i,k);

  25. cout <<" | " ;

  26. }

  27. cout<<endl;

  28. }

  29. return 0 ;

  30. }

g++ test.cpp -o test -fPIC -DHAVE_NAMESPACE_STD -DHAVE_CXX_STRING_HEADER -DDLLIMPORT="" -I /opt/PostgreSQL/9.3/include -L/opt/PostgreSQL/9.3/lib -lpq++ -lpq

./test


你可能感兴趣的:(ubuntu14.04下使用libpq++)