설문조사
PostgreSQL/PPAS 관련 듣고 싶은 교육은


Powered by EnterpriseDB
총 게시물 13건, 최근 0 건
   

C++ 과 연동하기

글쓴이 : PostgresDBA 날짜 : 2012-12-15 (토) 11:26 조회 : 5373
libpqxx 는 PostgreSQL 에 대한 공식 C++ 클라이언트 API 입니다.

http://pqxx.org/development/libpqxx/wiki/DownloadPage
에서 소스를 다운받아 컴파일하여 설치할수 있습니다.

참고로 아래에서 보이는 libpq 는 C 클라이언트 API 입니다. (PostgreSQL 설치시에 디폴트로 설치되네요)
C 인터페이스는 여기서 다루지 않겠습니다.

[root@olmaster:/usr/local/src/libpqxx-4.0]# rpm -ql postgresql92-devel-9.2.1-1PGDG.rhel6.x86_64|grep libpq
/usr/pgsql-9.2/include/internal/libpq
/usr/pgsql-9.2/include/internal/libpq-int.h
/usr/pgsql-9.2/include/internal/libpq/pqcomm.h
/usr/pgsql-9.2/include/libpq
/usr/pgsql-9.2/include/libpq-events.h
/usr/pgsql-9.2/include/libpq-fe.h
/usr/pgsql-9.2/include/libpq/libpq-fs.h
/usr/pgsql-9.2/include/server/libpq
/usr/pgsql-9.2/include/server/libpq/auth.h
/usr/pgsql-9.2/include/server/libpq/be-fsstubs.h
/usr/pgsql-9.2/include/server/libpq/crypt.h
/usr/pgsql-9.2/include/server/libpq/hba.h
/usr/pgsql-9.2/include/server/libpq/ip.h
/usr/pgsql-9.2/include/server/libpq/libpq-be.h
/usr/pgsql-9.2/include/server/libpq/libpq-fs.h
/usr/pgsql-9.2/include/server/libpq/libpq.h
/usr/pgsql-9.2/include/server/libpq/md5.h
/usr/pgsql-9.2/include/server/libpq/pqcomm.h
/usr/pgsql-9.2/include/server/libpq/pqformat.h
/usr/pgsql-9.2/include/server/libpq/pqsignal.h
/usr/pgsql-9.2/lib/libpq.a
/usr/pgsql-9.2/lib/libpq.so
[root@olmaster:/usr/local/src/libpqxx-4.0]# 

http://pqxx.org/development/libpqxx/wiki/DownloadPage 
싸이트를 방문하여 현시점의 stable 버전인 libpqxx-4.0.tar.gz 를 다운로드 하였습니다.
아래 처럼 진행하여 설치를 완료합니다.

C++ 프로그래밍 테스트 환경은 CentOS 6.3 (x64) 입니다.

[root@olmaster:~]# cat /etc/redhat-release     ## redhat 기반이라서 아래와 같이 나오는군요.
Red Hat Enterprise Linux Server release 6.3 (Santiago)
[root@olmaster:~]# 

[root@olmaster:/usr/local/src]# ls -l
합계 2572
-rwxr-xr-x. 1 root root 2631181 2012-12-15 10:58 libpqxx-4.0.tar.gz*
[root@olmaster:/usr/local/src]# 
[root@olmaster:/usr/local/src]#  tar xvfz libpqxx-4.0.tar.gz
[root@olmaster:/usr/local/src]# ls -alrt
합계 2584
drwxrwxr-x. 10 1000 1000    4096 2011-11-27 20:14 libpqxx-4.0/
drwxr-xr-x. 12 root root    4096 2012-11-09 23:12 ../
-rwxr-xr-x.  1 root root 2631181 2012-12-15 10:58 libpqxx-4.0.tar.gz*
drwxr-xr-x.  3 root root    4096 2012-12-15 10:58 ./
[root@olmaster:/usr/local/src]# cd libpqxx-4.0
[root@olmaster:/usr/local/src/libpqxx-4.0]# 

-- 컴파일시에 pg_config 유틸리티를 호출하므로 반드시 아래 PATH 를 설정해야 합니다.
[root@olmaster:/usr/local/src/libpqxx-4.0]# export PATH=/usr/pgsql-9.2/bin:$PATH
[root@olmaster:/usr/local/src/libpqxx-4.0]# pg_config       # 절대경로없이 호출이 잘되네요.
BINDIR = /usr/pgsql-9.2/bin
DOCDIR = /usr/share/doc/pgsql
HTMLDIR = /usr/share/doc/pgsql
INCLUDEDIR = /usr/pgsql-9.2/include
PKGINCLUDEDIR = /usr/pgsql-9.2/include
INCLUDEDIR-SERVER = /usr/pgsql-9.2/include/server
LIBDIR = /usr/pgsql-9.2/lib
PKGLIBDIR = /usr/pgsql-9.2/lib
LOCALEDIR = /usr/pgsql-9.2/share/locale
MANDIR = /usr/pgsql-9.2/share/man
SHAREDIR = /usr/pgsql-9.2/share
SYSConFDIR = /etc/sysconfig/pgsql
PGXS = /usr/pgsql-9.2/lib/pgxs/src/makefiles/pgxs.mk
ConFIGURE = '--disable-rpath' '--prefix=/usr/pgsql-9.2' '--includedir=/usr/pgsql-9.2/include' '--mandir=/usr/pgsql-9.2/share/man' '--datadir=/usr/pgsql-9.2/share' '--with-perl' '--with-python' '--with-tcl' '--with-tclconfig=/usr/lib64' '--with-openssl' '--with-pam' '--with-krb5' '--with-gssapi' '--with-includes=/usr/include' '--with-libraries=/usr/lib64' '--enable-nls' '--with-ossp-uuid' '--with-libxml' '--with-libxslt' '--with-ldap' '--with-system-tzdata=/usr/share/zoneinfo' '--sysconfdir=/etc/sysconfig/pgsql' '--docdir=/usr/share/doc' 'CFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I/usr/include/et' 'CPPFLAGS= -I/usr/include/et'
CC = gcc
CPPFLAGS = -I/usr/include/et -D_GNU_SOURCE -I/usr/include/libxml2 -I/usr/include
CFLAGS = -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I/usr/include/et -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv
CFLAGS_SL = -fpic
LDFLAGS = -L/usr/lib64 -Wl,--as-needed
LDFLAGS_EX = 
LDFLAGS_SL = 
LIBS = -lpgport -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -lreadline -lcrypt -ldl -lm 
VERSION = PostgreSQL 9.2.1
[root@olmaster:/usr/local/src/libpqxx-4.0]# 

[root@olmaster:/usr/local/src/libpqxx-4.0]# ./configure --prefix=/usr/local/libpq4 --enable-shared
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
........
config.status: executing libtool commands
config.status: executing configitems commands
Checking for usable grep -F or equivalent... grep-F
Generating include/pqxx/config-internal-autotools.h: ok
Generating include/pqxx/config-internal-compiler.h: ok
Generating include/pqxx/config-internal-libpq.h: ok
Generating include/pqxx/config-public-autotools.h: no items--skipping
Generating include/pqxx/config-public-compiler.h: ok
Generating include/pqxx/config-public-libpq.h: no items--skipping
[root@olmaster:/usr/local/src/libpqxx-4.0]# make
...
[root@olmaster:/usr/local/src/libpqxx-4.0]# make install
...
[root@olmaster:/usr/local/src/libpqxx-4.0]# cd ../../libpq4/
[root@olmaster:/usr/local/libpq4]# find . -type d        # 여러 디렉토리가 보이는것 보니깐, 설치가 잘 됐네요
.
./bin
./lib
./lib/pkgconfig
./include
./include/pqxx
./include/pqxx/internal
./include/pqxx/internal/gates
[root@olmaster:/usr/local/libpq4]# 

libpq 라이브러리까지 설치가 잘되었으니 이제 초간단 샘플 프로그램을 만들어보겠습니다.

[postgres@olmaster:~/oradba]$ cat sample.cpp   ## 샘플 소스는 다음과 같습니다.
#include <iostream>
#include <pqxx/pqxx>

/*
(c) copyright PostgresDBA.COM
*/

int main()
{
  pqxx::connection c("dbname=scottdb host=10.10.10.94 user=scott password=tiger");
  pqxx::work txn(c);

  pqxx::result employees = txn.exec("SELECT empno, ename FROM emp");
  for (pqxx::result::const_iterator row = employees.begin(); row != employees.end(); ++row)
  {
      std::cout
        << row["empno"].as<int>() << "\t"
        << row["ename"].as<std::string>()
        << std::endl;
  }
  return 0;
}
[postgres@olmaster:~/oradba]$ 

-- 이제 본격적으로 컴파일 해봅시다.

[postgres@olmaster:~/oradba]$ g++ sample.cpp -I/usr/local/libpq4/include/  -L/usr/local/libpq4/lib -L/usr/pgsql-9.2/lib -lpqxx -lpq  -o sample.out

[postgres@olmaster:~/oradba]$ ls -l ./sample.out
-rwxr-xr-x. 1 postgres postgres 51303 2012-12-15 12:20 ./sample.out*
[postgres@olmaster:~/oradba]$ ./sample.out       ## 이런 shared library 를 못찾는군요.
./sample.out: error while loading shared libraries: libpqxx-4.0.so: cannot open shared object file: No such file or directory
[postgres@olmaster:~/oradba]$

-- root 유저로 로그인하여 library path 를 추가하여 줍니다.
[postgres@olmaster:~/oradba]$ su - root
[root@olmaster:/]# cd /etc/ld.so.conf.d
[root@olmaster:/etc/ld.so.conf.d]# cat user.conf 
/usr/local/libpq4/lib
[root@olmaster:/etc/ld.so.conf.d]# ldconfig

-- 이제 다시 sample.out 를 수행해보겠습니다.
[root@olmaster:/etc/ld.so.conf.d]# su - postgres
[postgres@olmaster:~/oradba]$ ./sample.out        ## 와우 !! 성공했네요.
7369    SMITH
7499    ALLEN
7521    WARD
7566    JONES
7654    MARTIN
7698    BLAKE
7782    CLARK
7788    SCOTT
7839    KING
7844    TURNER
7876    ADAMS
7900    JAMES
7902    FORD
7934    MILLER
[postgres@olmaster:~/oradba]$ 

-- sample.out 화일을 살펴봤더니 64bit 로 컴파일된 바이너리군요.
[postgres@olmaster:~/oradba]$ file sample.out 
sample.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped

[postgres@olmaster:~/oradba]$ ldd sample.out 
        linux-vdso.so.1 =>  (0x00007fff673ff000)
        libpqxx-4.0.so => /usr/local/libpq4/lib/libpqxx-4.0.so (0x00007f2d1f4c1000)
        libpq.so.5 => /usr/pgsql-9.2/lib/libpq.so.5 (0x00007f2d1f293000)
        libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003af5200000)
        libm.so.6 => /lib64/libm.so.6 (0x0000003ae8200000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003af4200000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003ae8600000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003ae8e00000)
        libssl.so.10 => /usr/lib64/libssl.so.10 (0x0000003af6a00000)
        libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x0000003af3600000)
        libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x0000003af5600000)
        libldap_r-2.4.so.2 => /lib64/libldap_r-2.4.so.2 (0x00007f2d1f03d000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003ae7e00000)
        libkrb5.so.3 => /lib64/libkrb5.so.3 (0x0000003af4e00000)
        libcom_err.so.2 => /lib64/libcom_err.so.2 (0x0000003af2e00000)
        libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x0000003af3a00000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003ae8a00000)
        libz.so.1 => /lib64/libz.so.1 (0x0000003ae9200000)
        libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x0000003af3e00000)
        libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x0000003af4a00000)
        libresolv.so.2 => /lib64/libresolv.so.2 (0x0000003aea200000)
        liblber-2.4.so.2 => /lib64/liblber-2.4.so.2 (0x0000003afc200000)
        libssl3.so => /usr/lib64/libssl3.so (0x0000003af8a00000)
        libsmime3.so => /usr/lib64/libsmime3.so (0x0000003af8200000)
        libnss3.so => /usr/lib64/libnss3.so (0x0000003af7600000)
        libnssutil3.so => /usr/lib64/libnssutil3.so (0x00007f2d1ee13000)
        libplds4.so => /lib64/libplds4.so (0x0000003af6e00000)
        libplc4.so => /lib64/libplc4.so (0x0000003af7200000)
        libnspr4.so => /lib64/libnspr4.so (0x0000003af7a00000)
        libsasl2.so.2 => /usr/lib64/libsasl2.so.2 (0x0000003bfcc00000)
        libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003ae9e00000)
        libcrypt.so.1 => /lib64/libcrypt.so.1 (0x0000003796a00000)
        libfreebl3.so => /lib64/libfreebl3.so (0x0000003af6600000)
[postgres@olmaster:~/oradba]$ 

이로서 험난한 과정이 끝났습니다.
조금 어렵죠? ^^

   

postgresdba.com