行业知识
【构建依赖库】libpqxx 库安装及使用
���录
1、下载源码包
2、解决Can't find the main PostgreSQL client header
3、构建安装
4、构建使其生成动态库
5、psql连接测试
1、下载源码包
libpqxx 是基于 PostgreSQL C 客户端库 libpq 实现的一个 C++ 封装库,在 C++ 程序中可以更方便地操作 PostgreSQL 数据库。官网:
GitHub - jtv/libpqxx: The official C++ client API for PostgreSQL.The official C++ client API for PostgreSQL. Contribute to jtv/libpqxx development by creating an account on GitHub.https://github.com/jtv/libpqxx如果你的系统没有提供预编译的包,或者需要使用更高版本的 libpqxx,可以从官方网站下载源代码进行编译安装。在 Linux 或 macOS 系统上,你可以按照以下步骤进行编译安装:
# 下载源码包wget https://github.com/jtv/libpqxx/archive/7.4.1.tar.gz# 解压源码包tar xzf 7.4.1.tar.gz# 进入源码目录cd libpqxx-7.4.1
# 编译和安装:指定安装位置
./configure --prefix=/home/cjs/algorithm/pq
遇到问题:
checking libpq-fe.h usability... no
checking libpq-fe.h presence... no
checking for libpq-fe.h... no
configure: error:
Can't find the main PostgreSQL client header, libpq-fe.h. Are you sure the
libpq headers are installed correctly, and that we've got the right path?
2、解决Can't find the main PostgreSQL client header
如果你在编译或安装 PostgreSQL 应用程序时遇到了以下错误:
Can't find the main PostgreSQL client header, libpq-fe.h.
那么这通常意味着你的系统缺少 PostgreSQL 客户端开发包,其中包括 libpq-fe.h 头文件。
要解决这个问题,你需要安装 PostgreSQL 客户端开发包。具体方法取决于你的操作系统和安装方式。
如果你使用的是 Ubuntu 或 Debian 发行版,请尝试运行以下命令来安装:
sudo apt-get install libpq-dev
即可解决问题。然后再次执行:
./configure --prefix=/home/cjs/algorithm/pq
3、构建安装
make
sudo make install
结果展示:
生成.a文件
4、构建使其生成动态库
mkdir buildcd buildcmake -DBUILD_SHARED_LIBS=ON ..sudo make install DESTDIR=/home/cjs/algorithm/pqd
生成.so文件
5、psql连接测试
psql数据库:
建立qt的C++工程:
main.cpp
#include #include "pqxx/pqxx"using namespace std;int main(){ std::string conn_string = "hostaddr=填写目的ip地址 port=30061 dbname=test user=postgres password=fjf"; pqxx::connection conn(conn_string); pqxx::work txn(conn); pqxx::result result = txn.exec("SELECT * FROM fan"); txn.commit(); // Process results for(auto row_it = result.begin(); row_it != result.end(); row_it++) { std::cout
上一篇:揭秘水井坊酒最新价格表!
下一篇:电脑代码编辑器教程