跳至主要内容

【转帖】berkeleyDB安装

我们以redhat9为例开始介绍其安装过程:

从其sleepycat公司官网的下载页http://dev.sleepycat.com/downloads/releasehistorybdb.html获得其安装包,现在已经更新到4.4.20.
  #tar zxfv db-4.x.tgz
  #cd db-4.x/build_unix

  1. 如果以gcc编译的话,进行以下操作:
  #vi /dist/configure 在最前面添加:CC gcc
  #../dist/configure
  #make
  #make install
  默认状态,berkeleyDB的lib和include将被安装到/usr/local/BerkeleyDB/下,需要更改这个路径的话,可以将 #../dist/configure一步加上选项--prefix,例如:#../dist/configure --prefix=/opt/BerkeleyDB.
  #vi /etc/ld.so.conf 并将berkeleyDB的lib路径加到该文件的最后一行,这样才能找到并加载它的库文件.ld.so.conf是什么东西?它就是系统动态链接库的配置文件。此文件内,存放着可被LINUX共享的动态链接库所在目录的名字(系统目录/lib,/usr/lib除外),各个目录名间以空白字符(空格,换行等)或冒号或逗号分隔。
  #ldconfig

  2. 如果以armgcc编译的话,进行以下操作:
  先安装armgcc,对我说就是在/mnt/setup/program/FFT/FFT-2410光盘-V3.0/linux开发/linux交叉编译器/tool或者在gmail中附件里armgcc目录下(除了cross-armv4l-qtE-custom-1.5-3mz.i386.rpm,其他都装)
  #cp /mnt/setup/program/FFT/FFT-2410光盘-V3.0/linux开发/linux交叉编译器/tool /usr/tmp/
  #cd /usr/tmp/tool
  #rpm -ivh *
  #ldconfig
  这一系列的工具将默认被装到/opt/host/arm41下.

  安装db-4.4.20.tgz,你甚至可以直接修改configure文件.而对于4.3.29,则不可直接修改configure文件.
  #tar zxfv db-4.x.tgz
  #cd db-4.x/build_unix
 
  #vi /dist/configure
  在最前面添加以下几行:
  CC=/opt/host/armv4l/bin/armv4l-unknown-linux-gcc
  AR=/opt/host/armv4l/bin/armv4l-unknown-linux-ar
  RANLIB=/opt/host/armv4l/bin/armv4l-unknown-linux-ranlib
  STRIP=/opt/host/armv4l/bin/armv4l-unknown-linux-strip

  #../dist/configure --prefix=/opt/db --host=arm41-unknown-linux(这样的话,berkeleyDB的lib和include将被装到/opt/db下.)

  或者:
  #env CC=/opt/host/armv4l/bin/armv4l-unknown-linux-gcc \
  AR=/opt/host/armv4l/bin/armv4l-unknown-linux-ar \
  RANLIB=/opt/host/armv4l/bin/armv4l-unknown-linux-ranlib \
  STRIP=/opt/host/armv4l/bin/armv4l-unknown-linux-strip \
  ../dist/configure --prefix=/opt/db --host=arm41-unknown-linux

  #make
  #make install
  #vi /etc/ld.so.conf 并将berkeleyDB的lib路径加到该文件的最后一行,在这里就是/opt/db/lib
  #ldconfig

详见: http://www.sleepycat.com/docs/ref/build_unix/intro.html
附上一段经典的检测是否装好的代码,同时也可以利用该代码入门:
#include <db.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

/* DB的函数执行完成后,返回0代表成功,否则失败 */
void print_error(int ret)
{
  if(ret != 0)
    printf("ERROR: %s\n",db_strerror(ret));
}

/* 数据结构DBT在使用前,应首先初始化,否则编译可通过但运行时报参数错误 */
void init_DBT(DBT * key, DBT * data)
{
  memset(key, 0, sizeof(DBT));
  memset(data, 0, sizeof(DBT));
}

void main(void)
{
  DB *dbp;      
  DBT key, data;
  u_int32_t flags;
  int ret;

  char *fruit = "apple";
  int number = 15;
 
  typedef struct customer
  {
    int c_id;
    char name[10];
    char address[20];
    int age;
  } CUSTOMER;
  CUSTOMER cust;
  int key_cust_c_id = 1;

  cust.c_id = 1;
  strncpy(cust.name, "javer", 9);
  strncpy(cust.address, "chengdu", 19);
  cust.age = 32;

  /* 首先创建数据库句柄 */
  ret = db_create(&dbp, NULL, 0);
  print_error(ret);
 
  /* 创建数据库标志 */
  flags = DB_CREATE;  

  /* 创建一个名为single.db的数据库,使用B+树访问算法,本段代码演示对简单数据类型的处理 */
  ret = dbp->open(dbp, NULL, "single.db", NULL, DB_BTREE, flags, 0);
  print_error(ret);

  init_DBT(&key, &data);
 
  /* 分别对关键字和数据赋值和规定长度 */
  key.data = fruit;
  key.size = strlen(fruit) + 1;
  data.data = &number;
  data.size = sizeof(int);

  /* 把记录写入数据库中,不允许覆盖关键字相同的记录 */
  ret = dbp->put(dbp, NULL, &key, &data,DB_NOOVERWRITE);
  print_error(ret);
 
/* 手动把缓存中的数据刷新到硬盘文件中,实际上在关闭数据库时,数据会被自动刷新 */
dbp->sync();
 
init_DBT(&key, &data);

  key.data = fruit;
  key.size = strlen(fruit) + 1;
 
  /* 从数据库中查询关键字为apple的记录 */
  ret = dbp->get(dbp, NULL, &key, &data, 0);
  print_error(ret);

  /* 特别要注意数据结构DBT的字段data为void *型,所以在对data赋值和取值时,要做必要的类型转换。 */
  printf("The number = %d\n", *(int*)(data.data));
 
  if(dbp != NULL)
        dbp->close(dbp, 0);

  ret = db_create(&dbp, NULL, 0);
  print_error(ret);

  flags = DB_CREATE;  

  /* 创建一个名为complex.db的数据库,使用HASH访问算法,本段代码演示对复杂数据结构的处理 */
  ret = dbp->open(dbp, NULL, "complex.db", NULL, DB_HASH, flags, 0);
  print_error(ret);

  init_DBT(&key, &data);

  key.size = sizeof(int);
  key.data = &(cust.c_id);

  data.size = sizeof(CUSTOMER);
  data.data = &cust;

  ret = dbp->put(dbp, NULL, &key, &data,DB_NOOVERWRITE);
  print_error(ret);
 
  memset(&cust, 0, sizeof(CUSTOMER));
 
  key.size = sizeof(int);
  key.data = &key_cust_c_id;

  data.data = &cust;
  data.ulen = sizeof(CUSTOMER);
  data.flags = DB_DBT_USERMEM;

  dbp->get(dbp, NULL, &key, &data, 0);
  print_error(ret);
 
  printf("c_id = %d name = %s address = %s age = %d\n",
    cust.c_id, cust.name, cust.address, cust.age);
 
  if(dbp != NULL)
        dbp->close(dbp, 0);
}
我们以gcc编译为例:
gcc test.c -ggdb -I/usr/local/BerkeleyDB.4.3/include/ -L/usr/local/BerkeleyDB.4.3/lib/ -ldb -lpthread,如果你用的是eclipse之类的工具的话,记得加上-I,-L,-l的选项哦.
如果运行时报错:error while loading shared libraries:libdb-4.3.so: cannot open shared object file: No such file or directory的错误(小乌碰到过),请检查一下是否把berkeleyDB的lib路径加入到/etc/ld.so.conf中.

评论

此博客中的热门博文

【转】smb协议栈使用示例

/*  * * uncdownload.c  * *  * * Utility for downloading files from SMB shares using libsmbclient  * *  * * Copyright(C) 2006 Sophos Plc, Oxford, England.  * *  * * This program is free software; you can redistribute it and/or modify it under the terms of the  * * GNU General Public License Version 2 as published by the Free Software Foundation.  * *  * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without  * * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  * * See the GNU General Public License for more details.  * *  * * You should have received a copy of the GNU General Public License along with this program; if not,  * * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  * *  * */ # include < sys / types . h > # include < sys / time . h > # include ...

【转】Ether Types

Ether Types (last updated 2008-09-09) NOTE: Please see [RFC5342] for current information and registration procedures. This registry will be revised soon and will be replaced with up-to-date information. Many of the networks of all classes are Ethernets (10Mb) or Experimental Ethernets (3Mb). These systems use a message "type" field in much the same way the ARPANET uses the "link" field. If you need an Ether Type, contact: IEEE Registration Authority IEEE Standards Department 445 Hoes Lane Piscataway, NJ 08854 Phone +1 732 562 3813 Fax: +1 732 562 1571 Email: <ieee-registration-authority& ieee.org > http://standards.ieee.org/regauth/index.html The following list of EtherTypes is contributed unverified information from various sources. Another list of EtherTypes is maintained by Michael A. Patton and is accessible at: <URL: http://www.cavebear.com/CaveBear/Ethernet/ > <URL: ftp://ftp.cavebear.com/pub/Ethernet-codes > Assign...

【转】udp编程实例

UDP通讯实例 2008-04-29 15:30:05 / 个人分类: linux C编程 UDP协议的几点好处: 1.UDP不要求保持一个连接; 2.UDP没有因接收方认可收到数据包(或者当数据包没有正确抵达而自动重传)而带来的开销; 3.设计UDP的目的是用于短应用和控制信息; 4.在一个数据包接一个数据包的基础上,UDP要求的 网络 带宽比TCP更小。 UDP的几个缺点: 1.程序员必须创建代码检测传输错误并进行重传(如果应用程序要求这样做); 2.程序员必须把大数据包分片。 code: <1> /* * sender.c--UDP protocol example */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> int port = 6789; int main() {     int socket_descrīptor;     int iter = 0;     char buf[80];     struct sockaddr_in address;     /* Initialize socket address structure for Interner Protocols */     bzero(&address, sizeof(address)); // empty data structure     address.sin_family = AF_INET;     address.sin_addr.s_addr = inet_addr("127.0.0.1");     address...