Ubuntu安装BerkeleyDB

Ubuntu安装Berkeley DB

Ubuntu 系统版本: Ubuntu 16.04 LTS
Berkeley DB 版本: Berkeley DB 18.1.25

下载Berkeley DB源代码

https://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html

解压缩文件:

tar -xvf db-18.1.25.tar.gz

进入目录build_unix:

cd build_unix

自动配置,生成Makefile(默认配置不会生成cxx支持库):

…/dist/configure

添加CXX支持,在配置后添加参数“–enalbe-cxx”:

…/dist/configure --enable-cxx

编译源代码:

make

安装:

sudo make install

卸载与重新配置编译命令为:

sudo make uninstall
make realclean

Berkeley DB默认安装路径为:

/usr/local/BerkeleyDB.18.1

在使用时,需要在添加头文件和链接库路径,如:

g++ *.cpp -I"/usr/local/BerkeleyDB.18.1/include" -L"/usr/local/BerkeleyDB.18.1/lib/" -ldb_cxx

链接动态链接库libdb_cxx-18.1.so到/usr/local/lib:

sudo ln /usr/local/BerkeleyDB.18.1/lib/libdb_cxx-18.1.so /usr/local/lib/libdb_cxx-18.1.so
sudo ldconfig

或者在/etc/ld.so.conf.d/目录下添加BerkeleyDB链接库路径:

sudo echo /usr/local/BerkeleyDB.18.1/lib>BerkeleyDB.conf
sudo ldconfig

访问示例:
复制文件 db-18.1.25/examples/cxx/AccessExample.cpp到临时目录:

/*-
 * Copyright (c) 1997, 2018 Oracle and/or its affiliates.  All rights reserved.
 *
 * See the file EXAMPLES-LICENSE for license information.
 *
 * $Id$
 */

#include 

#include 
#include 
#include 
#include 
#include 

#ifdef _WIN32
extern "C" {
  extern int getopt(int, char * const *, const char *);
  extern int optind;
}
#else
#include 
#endif

#include 

#define	DATABASE	"access.db"

using std::cin;
using std::cout;
using std::cerr;

class AccessExample
{
public:
	AccessExample();
	void run(bool removeExistingDatabase, const char *fileName);

private:
	// no need for copy and assignment
	AccessExample(const AccessExample &);
	void operator = (const AccessExample &);
};

int
usage()
{
	(void)fprintf(stderr, "usage: AccessExample [-r] [database]\n");
	return (EXIT_FAILURE);
}

int
main(int argc, char *argv[])
{
	int ch, rflag;
	const char *database;

	rflag = 0;
	while ((ch = getopt(argc, argv, "r")) != EOF)
		switch (ch) {
		case 'r':
			rflag = 1;
			break;
		case '?':
		default:
			return (usage());
		}
	argc -= optind;
	argv += optind;

	/* Accept optional database name. */
	database = *argv == NULL ? DATABASE : argv[0];

	// Use a try block just to report any errors.
	// An alternate approach to using exceptions is to
	// use error models (see DbEnv::set_error_model()) so
	// that error codes are returned for all Berkeley DB methods.
	//
	try {
		AccessExample app;
		app.run((bool)(rflag == 1 ? true : false), database);
		return (EXIT_SUCCESS);
	}
	catch (DbException &dbe) {
		cerr << "AccessExample: " << dbe.what() << "\n";
		return (EXIT_FAILURE);
	}
}

AccessExample::AccessExample()
{
}

void AccessExample::run(bool removeExistingDatabase, const char *fileName)
{
	// Remove the previous database.
	if (removeExistingDatabase)
		(void)remove(fileName);

	// Create the database object.
	// There is no environment for this simple example.
	Db db(0, 0);

	db.set_error_stream(&cerr);
	db.set_errpfx("AccessExample");
	db.set_pagesize(1024);		/* Page size: 1K. */
	db.set_cachesize(0, 32 * 1024, 0);
	db.open(NULL, fileName, NULL, DB_BTREE, DB_CREATE, 0664);

	//
	// Insert records into the database, where the key is the user
	// input and the data is the user input in reverse order.
	//
	char buf[1024], rbuf[1024];
	char *p, *t;
	int ret;
	u_int32_t len;

	for (;;) {
		cout << "input> ";
		cout.flush();

		cin.getline(buf, sizeof(buf));
		// 修改为终端输入exit推出循环
		//if (cin.eof())
		if(strcmp(buf,"exit")==0)
			break;

		if ((len = (u_int32_t)strlen(buf)) <= 0)
			continue;
		for (t = rbuf, p = buf + (len - 1); p >= buf;)
			*t++ = *p--;
		*t++ = '\0';

		Dbt key(buf, len + 1);
		Dbt data(rbuf, len + 1);

		ret = db.put(0, &key, &data, DB_NOOVERWRITE);
		if (ret == DB_KEYEXIST) {
			cout << "Key " << buf << " already exists.\n";
		}
	}
	cout << "\n";

	// We put a try block around this section of code
	// to ensure that our database is properly closed
	// in the event of an error.
	//
	try {
		// Acquire a cursor for the table.
		Dbc *dbcp;
		db.cursor(NULL, &dbcp, 0);

		// Walk through the table, printing the key/data pairs.
		Dbt key;
		Dbt data;
		while (dbcp->get(&key, &data, DB_NEXT) == 0) {
			char *key_string = (char *)key.get_data();
			char *data_string = (char *)data.get_data();
			cout << key_string << " : " << data_string << "\n";
		}
		dbcp->close();
	}
	catch (DbException &dbe) {
		cerr << "AccessExample: " << dbe.what() << "\n";
	}

	db.close(0);
}

编译:

g++ -o AccessExample AccessExample.cpp -I /usr/local/BerkeleyDB.18.1/include/ -L /usr/local/BerkeleyDB.18.1/lib/ -ldb_cxx

运行:

./AccessExample
input> 123
input> 456
input> 789
input> abc
input> d
input> efg
input> exit

123 : 321
456 : 654
789 : 987
abc : cba
d : d
efg : gfe

程序自动生成一个access.db的文件,并将终端输入数据存储,即数据库文件。

你可能感兴趣的:(Bitcoin)