首页 > Berkeley DB XML > Berkeley DB Xml 入门系列之二: 在Linux下编译和使用Berkeley DB Xml

Berkeley DB Xml 入门系列之二: 在Linux下编译和使用Berkeley DB Xml

2009年7月22日 蔡瀛

在前面的文章《Berkeley DB Xml 入门系列之一: 简介与”query”示例程序详解》我们已经知道了Berkeley DB Xml程序的一些基本流程和概念。在文章《使用Visual C++ 2008 Express Edition 编译Oracle Berkeley DB XML》介绍了如何使用Visual Studio 在Windows平台上编译Berkeley DB Xml,本文将介绍在Linux或者类Unix的操作系统上如何编译和使用Berkeley DB Xml。

下载

可以在这个地址http://www.oracle.com/technology/software/products/berkeley-db/xml/index.html下载最新的Berkeley DB Xml的 安装包,Linux下使用可以下载.tar.gz 或者.zip结尾的包。目前的最新版本是2.4.16, .tar.gz结尾的包的大小约为36M。我们下载.tar.gz 结尾的包作为本文的示例。

下载完后得到是这样一个文件

ying@bdbcn3:~t$ ls
dbxml-2.4.16.tar.gz

编译

解压缩

ying@bdbcn3:~$ tar zxvf dbxml-2.4.16.tar.gz

进入dbxml-2.4.16目录后可以看见有一个buildall.sh的脚本, 这个脚本就是用来编译dbxml的。可以使用–help选项查看编译选项。一些主要选项有:用 -x 指定c++ 编译器, -c指定 c编译器,要注意的是Berkeley DB Xml需要使用gnu的make程序进行编译,可以用-m 指定make程序。 我们在linux下编译的话这些选项基本不需要指定,默认即可。

ying@bdbcn3:~/dbxml-2.4.16$ ./buildall.sh –help
Usage: ./buildall.sh [options]
–help                                 print this help message
-p <platform>                         platform type as understood by
runConfigure in Xerces-C++ [guessed]
-c <C compiler name>                 [gcc]
-x <C++ compiler name>               [g++]
-m <make command>                  [make]
-b <bits to build: 32 or 64>           [32]
–no-configure:                       do not re-run configure steps.  Assumes
configuration has been run previously
–clean:                               perform ‘make clean’ on all libraries
….

在运行buildall.sh的时候如果不指定安装目录,则所有编译产生的库文件和头文件就会安装在主目录的install目录下。如果指定了安装目录, 请确保对目录有写权限。我们把–enable-java的选项打开,编译java库。

ying@bdbcn3:~/dbxml-2.4.16$ ./buildall.sh –enable-java

接着等待编译过程,一般酷睿2的机器十几分钟就编译完了。查看安装目录

ying@bdbcn3:~t/dbxml-2.4.16$ cd install/
ying@bdbcn3:~/dbxml-2.4.16/install$ ls
bin  docs  include  lib

我们所需要的库就安装在lib目录下,头文件在include目录下,docs目录下是文档,bin目录下放了一些Berkeley DB 和 Berkeley DB Xml的工具。

编译C++ hello world程序

我们可以编译dbxml/examples/cxx/basic/helloWorld.cpp示例程序,看我们编译的库是否正确。在用编译的时候需要指定-I 和-L选项

ying@bdbcn3:~/dbxml-2.4.16$g++ helloWorld.cpp -I /home/ying/dbxml-2.4.16/install/include/ -L /home/ying/dbxml-2.4.16/install/lib/ -ldbxml -ldb_cxx -lxqilla -lxerces-c -o helloWorld

运行的时候需要把库的位置加入到环境变量中

ying@bdbcn3:~/dbxml-2.4.16$ export LD_LIBRARY_PATH=/home/ying/dbxml-2.4.16/install/lib/
ying@bdbcn3:~/dbxml-2.4.16$ ./helloWorld
Document name: doc
Content: <hello>Hello World</hello>
ying@bdbcn3:~/dbxml-2.4.16$

在一些linux发行版中直接安装

一些Linux发行版对Berkeley DB Xml已经进行了打包,可以直接安装使用非常方便。可以参考如下命令:

Feodra: sudo yum install dbxml
Gentoo: sudo emerge dbxml

总结

使用buildall.sh脚本来编译Berkeley db xml方便快捷,如果读者在linux或unix系统下编译有什么问题欢迎和我交流。

  1. 2009年7月27日23:00 | #1

    写的不错,有机会实验一下

  2. Ken
    2009年7月28日13:06 | #2

    好像编译没那么快……我的小黑编译了20分钟还没完

  3. 蔡瀛
    2009年7月28日17:52 | #3

    @Ken
    :)我用的dell620, 4G内存,Federo11 GCC4.4差不多20分钟左右

  4. chaohuang
    2009年7月29日10:11 | #4

    @Ken
    Hi Ken, 不妨考虑给你的小黑换个SSD试试,应该会快很多。当然,前提是你的小黑还不是一颗SSD的“心”(硬盘)。开个玩笑了… :-)

  5. 2009年8月11日16:54 | #5

    请教一下,下面安装出错是怎么回事? 系统是Hiweed ubuntu 2.0 rc3

    16:53:11 titi@titi-laptop:dbxml-2.4.16$ sh buildall.sh
    Start DB build: 2009年 08月 11日 星期二 16:53:18 CST
    Configuring Berkeley DB
    configure: WARNING: you should use –build, –host, –target
    configure: WARNING: invalid host type: dbXML/dbxml-2.4.16/install
    checking build system type… Invalid configuration `dbXML/dbxml-2.4.16/install’: machine `dbXML/dbxml’ not recognized
    configure: error: /bin/bash ../dist/config.sub dbXML/dbxml-2.4.16/install failed
    16:53:18 titi@titi-laptop:dbxml-2.4.16$

  6. 2009年8月11日17:08 | #6

    刚才再仔细看了一下出错信息,原来是父目录”Berkeley dbXML” 有个空格导致的。现在好了。提醒一下后来人注意。

  7. 2009年8月12日00:11 | #7

    下一步仍然出错,按照instruction安装python的binding。
    build/install bsddb3 package
    $ cd dbxml-2.x/dbxml/src/python/bsddb3-4.x
    $ python setup.dbxml.py build
    $ python setup.dbxml.py install
    You can then build and install the dbxml module:
    $ cd ..
    $ python setup.py build
    $ python setup.py install

    贴出来请教一下:
    laptop:python$ python setup.py build
    running build
    running build_py
    running build_ext
    building ‘_dbxml’ extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I../../include -I/media/sda2/software/@develop/database/BerkeleydbXML/dbxml-2.4.16/install/include -I/usr/include/python2.5 -c dbxml_python_wrap.cpp -o build/temp.linux-i686-2.5/dbxml_python_wrap.o
    cc1plus: 警告: 命令行选项“-Wstrict-prototypes”对 Ada/C/ObjC 是有效的,但对 C++ 无效
    dbxml_python_wrap.cpp:146:20: error: Python.h: 没有该文件或目录
    dbxml_python_wrap.cpp:2830:23: error: pythread.h: 没有该文件或目录
    dbxml_python_wrap.cpp:3007:4: error: #error “This python version requires swig to be run with the ‘-classic’ option”
    dbxml_python_wrap.cpp:751: 错误: expected initializer before ‘*’ token
    dbxml_python_wrap.cpp:806: 错误: expected initializer before ‘*’ token
    dbxml_python_wrap.cpp:827: 错误: expected initializer before ‘*’ token
    In file included from /usr/include/c++/4.3/new:45,
    from /usr/include/c++/4.3/ext/new_allocator.h:37,
    from /usr/include/c++/4.3/i486-linux-gnu/bits/c++allocator.h:39,
    from /usr/include/c++/4.3/bits/allocator.h:53,
    from /usr/include/c++/4.3/string:48,
    from dbxml_python_wrap.cpp:2502:
    /usr/include/c++/4.3/exception:40: 错误: expected declaration before end of line
    error: command ‘gcc’ failed with exit status 1

  8. 蔡瀛
    2009年8月12日10:34 | #8

    @tt
    hi,你的python是不是装在/usr/include/python2.5?看那个目录下有没有Python.h和pythread.h文件。

  9. 天门冬
    2010年6月29日10:59 | #9

    你好,
    我在Ubuntu下安装dbxml-2.4.16.tar.gz的时候,报如下错误:
    ../src/items/Numeric.cpp: In static member function ’static const XMLCh* Numeric ::asDoubleString(Numeric::State, const MAPM&, int, const StaticContext*)’:
    ../src/items/Numeric.cpp:349: error: ’sprintf’ was not declared in this scope
    ../src/items/Numeric.cpp:360: error: ’sprintf’ was not declared in this scope
    mark[1]: *** [Numeric.lo] 错误 1
    mark[1]:正在离开目录 ‘/opt/Installable/dbxml-2.4.16/xqilla/build’
    mark: *** [all-recursive] 错误 1

    请问是什么问题?谢谢

本文的评论功能被关闭了.
Դ