• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

整理好的C/C++ 开源库及示例代码

OC/C/C++ 开心洋葱 1460次浏览 0个评论

== 8.2 传输层 ==

socket

socket 最早源自 BSD 系统,有时候也称“伯克利套接字”。

它已成了传输层网络编程的标准,主流的操作系统平台都支持,主流的 C/C++ 编译器也都内置了相关的库文件。

ACE

Docs:[http://www.dre.vanderbilt.edu/Doxygen/Stable/libace-doc/annotated.html]

ACE 前面已经介绍过。它提供了针对 socket 的更高层封装。

APR

Docs:[https://apr.apache.org/docs/apr/trunk/modules.html]

APR 前面已经介绍过。它提供了对 socket 的封装和增强。

POCO::Net

Docs:[http://pocoproject.org/docs/Poco.Net.html]

POCO 前面已经介绍过。它提供了针对 TCP 服务端的封装类。

== 8.3 标准的应用层 ==

=== 8.3.1 综合性的库 ===

cURL & libcurl

Home:[http://curl.haxx.se/libcurl/]

Links:[https://en.wikipedia.org/wiki/CURL Wikipedia] [https://zh.wikipedia.org/wiki/CURL 维基百科]

cURL 是一个功能很强的网络库/网络工具,支持 N 多应用层协议。下面是支持协议的列表(从它官网抄袭的)

DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2

它采用 C 语言开发,开发很活跃,支持非常多的操作系统平台。

关于 cURL,俺前几年写过一篇博文推荐它(在“[http://program-think.blogspot.com/2009/03/opensource-review-curl-library.html 这里]”)。

代码示例——IMAP 协议(邮件)#include
#include

int main()
{
curl_global_init(CURL_GLOBAL_ALL);

CURL* curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_USERNAME, “user”);
curl_easy_setopt(curl, CURLOPT_PASSWORD, “password”);

// This will list the folders within the user’s mailbox. If you want to
// list the folders within a specific folder, for example the inbox,
// then specify the folder as a path in the URL such as /INBOX
curl_easy_setopt(curl, CURLOPT_URL, “imap://imap.example.com”);

CURLcode res = curl_easy_perform(curl);
if(res != CURLE_OK) // Check for errors
{
fprintf(stderr, “curl_easy_perform() failed: %s\n”,
curl_easy_strerror(res));
}

curl_easy_cleanup(curl); // Always cleanup
}

curl_global_cleanup();
return 0;
}

cURLpp

Home:[http://www.curlpp.org/]

看名字就知道这是 cURL 的 C++ 封装。

POCO::Net

Docs:[http://pocoproject.org/docs/Poco.Net.html]

POCO 前面已经介绍过。它提供了几种常见应用层协议(HTTP、SMTP、POP3、FTP、NTP …)的封装类。

=== 8.3.2 HTTP ===

(关于“HTTP 协议”,请参见另一个大类:“Web 相关”)

=== 8.3.3 DNS ===

c-ares

Home:[http://c-ares.haxx.se/]

这是一个 C 语言开发的 DNS 封装库,支持异步 DNS 请求,跨多种操作系统。

对比官网域名可知,它跟 cURL 是一家子。除了 cURL/libcurl 用到它,还有一些知名开源项目(比如:Wireshark、node.js …)用到它。

=== 8.3.4 XMPP ===

([https://zh.wikipedia.org/wiki/XMPP XMPP] 的洋文全称是“Extensible Messaging and Presence Protocol”。这是一个标准化的 IM 交互协议)

Swiften

Home:[http://swift.im/swiften.html]

这是一个 C++ 语言开发的 XMPP 封装库。它同时也是 Swift 聊天客户端所用的后端。

它大量使用了 Boost 的子库(Signal、Bind、Optional、Smart Pointers …)。

QXmpp

Home:[https://github.com/qxmpp-project/qxmpp]

这是一个 C++ 语言开发的 XMPP 封装库。从它的名称可以看出——依赖了 Qt 框架(需要 Qt 4.5 或更高版本)。

== 8.4 自定义的应用层 ==

Protocol Buffers

Home:[https://developers.google.com/protocol-buffers/]

Links:[https://en.wikipedia.org/wiki/Protocol_Buffers Wikipedia]

它是 Google 开发的一个跨语言的库,用于传输业务数据时的“编码/解码”。其优点是:跨多种语言、高性能、向前兼容、向后兼容。

具体的使用,可以参考俺前几年写过的一篇博文(在“[http://program-think.blogspot.com/2009/05/opensource-review-protocol-buffers.html 这里]”)。

作为 Protocol Buffers 的发明者,Google 默认实现了三种编程语言(C++、Java、Python)对它的支持。除了 Google 官方提供的这三种语言,它还支持很多其它的编程语言(由第三方提供)。

Apache Thrift

Home:[https://thrift.apache.org/]

Links:[https://en.wikipedia.org/wiki/Apache_Thrift Wikipedia]

来自于 Apache 社区,提供了一种跨语言的通讯机制。

程序员通过 Thrift 的“接口定义语言”定义通讯协议格式,然后 Thrift 根据协议格式自动帮你生成服务端和客户端代码。

(在这个方面,它有点类似于 Google 的 Protocol Buffers)

== 8.5 网络库、框架、中间件 ==

Boost.Asio

Docs:[http://boost.org/libs/asio]

Boost 前面已经介绍过。这是 Boost 的其中一个子库,提供了异步网络通讯和异步 I/O。

代码示例——TCP Server#include
#include
#include

int main()
{
using boost::asio::ip::tcp;
try
{
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
while(true)
{
tcp::socket socket(io_service);
acceptor.accept(socket);

std::string msg = “Hello, world”;
boost::system::error_code ignored_err;
boost::asio::write(socket, boost::asio::buffer(msg), ignored_err);
}
}
catch(std::exception& err)
{
std::cerr << err.what() << std::endl; } return 0;}

ACE

Docs:[http://www.dre.vanderbilt.edu/Doxygen/Stable/libace-doc/annotated.html]

ACE 前面已经介绍过。它提供了很多种用于网络通讯的设计模式。

ZeroMQ(ØMQ)

Home:[http://www.zeromq.org/]

Links:[https://en.wikipedia.org/wiki/ZeroMQ Wikipedia] [https://zh.wikipedia.org/wiki/%C3%98MQ 维基百科]

ZeroMQ 是一个轻量级、跨平台的开源库,提供了高性能、异步的消息队列。采用 C++ 开发,提供了多种语言的绑定。

与传统的消息中间件不同,使用 ZeroMQ 不需要额外的“消息代理(message broker)”。

俺曾经写过一篇博文推荐它(在“[http://program-think.blogspot.com/2011/08/opensource-review-zeromq.html 这里]”)。

代码示例——TCP Server#include

int main()
{
zmq::context_t context(1);

zmq::socket_t responder(context, ZMQ_REP);
responder.connect(“tcp://localhost:5560”);

while(true)
{
// Wait for next request from client
std::string request = s_recv(responder);
std::cout << "Received request: " << request << std::endl; // Do some 'work' sleep(1); // Send reply back to client s_send(responder, "Hello, world"); }}

nanomsg

Home:[http://nanomsg.org/]

很类似 ZeroMQ 的库,比 ZMQ 更加轻量级。采用 C 开发,提供了多种语言的绑定。

API 完全参照 BSD socket 的风格和语义。

代码示例——Request/Reply#include
#include #include
#include
#include

int reply(const char* url)
{
int sock = nn_socket(AF_SP, NN_PULL);
assert(sock >= 0);
assert(nn_bind(sock, url) >= 0);
while(1)
{
char* msg = NULL;
int bytes = nn_recv(sock, &msg, NN_MSG, 0);
assert(bytes >= 0);
printf(“RECEIVED:\n%s\n”, msg);
nn_freemsg(msg);
}
}

int request(const char* url, const char* msg)
{
int sz_msg = strlen(msg) + 1; // ‘\0’
int sock = nn_socket(AF_SP, NN_PUSH);
assert(sock >= 0);
assert(nn_connect(sock, url) >= 0);
printf(“SENDING:\n%s\n”, msg);
int bytes = nn_send(sock, msg, sz_msg, 0);
assert(bytes == sz_msg);
return nn_shutdown(sock, 0);
}

ICE(Internet Communications Engine)

Home:[https://zeroc.com/]

Links:[https://en.wikipedia.org/wiki/Internet_Communications_Engine Wikipedia] [https://zh.wikipedia.org/wiki/ICE_(%E4%B8%AD%E9%97%B4%E4%BB%B6) 维基百科]

这是一个面向对象的通讯中间件,诞生于2002年。支持不同编程语言的通讯。

它的设计借鉴了 CORBA,好在没有 CORBA 那么复杂。

libevent

Home:[http://libevent.org/]

Links:[https://en.wikipedia.org/wiki/Libevent Wikipedia] [https://zh.wikipedia.org/wiki/Libevent 维基百科]

它提供了异步事件处理机制。在网络开发中,可以用它替代传统的“event loop”,有助于简化代码。

它被一些知名的开源项目使用(比如:[https://zh.wikipedia.org/wiki/Tor Tor]、[https://zh.wikipedia.org/wiki/Memcached memcached])。

代码示例——HTTP Server(本示例基于 ANSI C)#include
#include
#include
#include
#include
#include

void generic_request_handler(struct evhttp_request* req, void* arg)
{
struct evbuffer* return_buffer = evbuffer_new();

evbuffer_add_printf(return_buffer, “Thanks for the request!”);
evhttp_send_reply(req, HTTP_OK, “Client”, return_buffer);
evbuffer_free(return_buffer);
}

int main()
{
short http_port = 8080;
char* http_addr = “127.0.0.1”;
struct evhttp* http_server = NULL;

event_init();
http_server = evhttp_start(http_addr, http_port);
evhttp_set_gencb(http_server, generic_request_handler, NULL);

fprintf(stderr, “Server started on port %d\n”, http_port);
event_dispatch();

return 0;
}

代码示例——HTTP Server(本示例基于 C++ 11 标准)#include
#include
#include
#include

int main()
{
if(!event_init())
{
std::cerr << "Failed to init libevent." << std::endl; return -1; } char const SrvAddress[] = "127.0.0.1"; std::uint16_t SrvPort = 8080; std::unique_ptr Server(evhttp_start(SrvAddress, SrvPort), &evhttp_free);
if(!Server)
{
std::cerr << "Failed to init http server." << std::endl; return -1; } void (*OnReq)(evhttp_request* req, void*) = [] (evhttp_request* req, void*) { auto* OutBuf = evhttp_request_get_output_buffer(req); if(!OutBuf) { return; } evbuffer_add_printf(OutBuf, "

Hello, World!

“);
evhttp_send_reply(req, HTTP_OK, “”, OutBuf);
};

evhttp_set_gencb(Server.get(), OnReq, nullptr);
if(event_dispatch() == -1)
{
std::cerr << "Failed to run messahe loop." << std::endl; return -1; } return 0;}

libev

Home:[http://libev.schmorp.de/]

看名称就能猜到它跟 libevent 很像。实际上,你可以把 libev 视为 libevent 的竞争性替代品。

[http://www.gevent.org/ gevent] 官方博客的[http://blog.gevent.org/2011/04/28/libev-and-libevent/ 一篇文章]对比了这俩库的优缺点。

libuv

Home:[https://github.com/libuv/libuv]

Links:[https://en.wikipedia.org/wiki/Libuv Wikipedia]

它提供了跨平台的异步 I/O 机制。主要是为了提供给 [https://en.wikipedia.org/wiki/Node.js Node.js] 使用。

除了支持网络通讯,还支持:线程池、Windows 命名管道、Unix domain sockets、文件系统事件通知 ……

Dlib

Docs:[http://dlib.net/network.html]

Dlib 前面已经介绍过。它针对网络通讯,提供了比较高的抽象层。

—-

= 9 数据库 =

== 9.1 开源数据库 ==

MySQL

Home:[https://www.mysql.com/]

Links:[https://en.wikipedia.org/wiki/MySQL Wikipedia] [https://zh.wikipedia.org/wiki/MySQL 维基百科]

名气最大的开源数据库,诞生于1995年,采用 C 和 C++ 语言开发。如今隶属于 Oracle 公司。

PostgreSQL

Home:[http://postgresql.org/]

Links:[https://en.wikipedia.org/wiki/PostgreSQL Wikipedia] [https://zh.wikipedia.org/wiki/PostgreSQL 维基百科]

名气仅次于 MySQL 的开源数据库,诞生于1996年。采用 C 语言开发。

SQLite

Home:[http://sqlite.org/]

Links:[https://en.wikipedia.org/wiki/SQLite Wikipedia] [https://zh.wikipedia.org/wiki/SQLite 维基百科]

它是一个很优秀的嵌入式(进程内)数据库,非常轻量级,支持各种作系统平台。采用 C 语言开发。

俺前几年写过一篇博文推荐它(在“[http://program-think.blogspot.com/2009/04/how-to-use-sqlite.html 这里]”)。

MongoDB

Home:[https://www.mongodb.org/]

Links:[https://en.wikipedia.org/wiki/MongoDB Wikipedia]

这是近几年兴起的 NoSQL 数据库的一员。它本身是基于 C++ 和 C 开发的。

Redis

Home:[http://redis.io/]

Links:[https://zh.wikipedia.org/wiki/Redis 维基百科] [https://en.wikipedia.org/wiki/Redis Wikipedia]

诞生于2009年,是目前(2014~2015)最流行的键值存储数据库,基于 C 语言开发。

以性能高而著称,很多大型网站用到它(Twitter、GitHub、Flickr、Instagram、百度、新浪、腾讯、搜狐)

Berkeley DB(BDB)

Home:[http://www.oracle.com/us/products/database/berkeley-db/]

Links:[https://en.wikipedia.org/wiki/Berkeley_DB Wikipedia] [https://zh.wikipedia.org/wiki/Berkeley_DB 维基百科]

诞生于1994年,是一个很老牌的嵌入式(进程内)数据库,提供“键值存储”的功能,基于 C 语言开发。

开发 BDB 的公司于2006年被 Oracle 收购。

很多开源项目用到它。甚至 MySQL 也包含了一个基于 BDB 的存储后端。

LevelDB

Home:[https://github.com/google/leveldb]

Links:[https://en.wikipedia.org/wiki/LevelDB Wikipedia] [https://zh.wikipedia.org/wiki/LevelDB 维基百科]

它是 Google 基于 C++ 开发的 NoSQL 数据库,提供“键值存储”的功能。

号称速度很快,内置数据压缩(基于 [https://en.wikipedia.org/wiki/Snappy_(software) Snappy] 库)。

比特币项目用到它。Facebook 基于它开发出 RocksDB 数据库。

Firebird

Home:[http://www.firebirdsql.org/]

Links:[https://en.wikipedia.org/wiki/Firebird_%28database_server%29 Wikipedia] [https://zh.wikipedia.org/wiki/Firebird_(%E6%95%B0%E6%8D%AE%E5%BA%93) 维基百科]

它是2000年的时候,从 [https://en.wikipedia.org/wiki/Borland Borland] 公司的 InterBase 数据库派生出来的。

基于 C++ 开发,支持多种操作系统平台。

关于它有个插曲:Firefox 浏览器曾经用过“Firebird”这个名称,因为跟 Firebird 数据库同名,后来才改用 Firefox 这个名称。

ScyllaDB

Home:[http://www.scylladb.com/]

这是2015年新兴的 NoSQL 数据库,相当于是用 C++ 重写了(Java 开发的)Cassandra。

号称性能提高10倍,并且延迟极低。

== 9.2 数据库 API 的封装库 ==

=== 9.2.1 综合性的封装库 ===

OTL

Home:[http://otl.sourceforge.net/]

Links:[https://en.wikipedia.org/wiki/Oracle_Template_Library Wikipedia]

原生支持的数据库:Oracle、SQL Server、DB2、Informix、TimesTen, MAX/DB;另支持 ODBC。

它的特色是:全部代码都在一个头文件中。

代码示例——操作 Oracle 数据库#include
#include

#define OTL_ORA8 // Compile OTL 4.0/OCI8
#include // include the OTL 4.0 header file

int main()
{
otl_connect::otl_initialize();
try
{
otl_connect db;
db.rlogon(“scott/tiger”); // connect to Oracle
otl_cursor::direct_exec(
db,
“drop table test_tab”,
otl_exception::disabled // disable OTL exceptions
);

otl_cursor::direct_exec(
db,
“create table test_tab(f1 number, f2 varchar2(30))”
);
}
catch(otl_exception& err)
{
using namespace std;
// intercept OTL exceptions
cerr << err.msg << endl; // print error message cerr << err.stm_text << endl; // print SQL that caused the error cerr << err.var_info << endl; // print variable that caused the error } db.logoff(); // disconnect from Oracle return 0;}

=== 9.2.2 MySQL 封装库 ===

MySQL Connector C++

Home:[http://dev.mysql.com/doc/connector-cpp/en/]

这是 MySQL 官方提供的 C++ 封装。

代码示例——执行 SQL 语句sql::mysql::MySQL_Driver* driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();
sql::Connection* conn = driver->connect(“tcp://127.0.0.1:3306”, “user”, “password”);
sql::Statement* stmt = conn->createStatement();

stmt->execute(“USE EXAMPLE_DB”);
stmt->execute(“DROP TABLE IF EXISTS test”);
stmt->execute(“CREATE TABLE test(id INT, label CHAR(1))”);
stmt->execute(“INSERT INTO test(id, label) VALUES (1, ‘a’)”);

delete stmt;
stmt = NULL;
delete conn;
conn = NULL;

MySQL++

Home:[http://www.tangentsoft.net/mysql%2B%2B/]

这是个老牌的库,诞生于1998年,提供了 MySQL API 的 C++ 封装。

代码示例——执行 SQL 语句#include
#include

void query(db_name, server_name, user, password)
{
using namespace std;

mysqlpp::Connection conn(false);
if(!conn.connect(db_name, server_name, user, password))
{
cerr << "DB connection failed: " << conn.error() << endl; return; } mysqlpp::Query query = conn.query("select item from table1"); mysqlpp::StoreQueryResult sqr = query.store() if(!sqr) { cerr << "Failed to get item list: " << query.error() << endl; return; } mysqlpp::StoreQueryResult::const_iterator iter; for(iter=sqr.begin(); iter!=sqr.end(); ++iter) { mysqlpp::Row row = *iter; cout << '\t' << row[0] << endl; }}

POCO::Data::MySQL

Docs:[http://pocoproject.org/docs/package-MySQL.MySQL.html]

POCO 前面已经介绍过。它提供了 MySQL 的封装类

=== 9.2.3 PostgreSQL 封装库 ===

libpq

Home:[http://www.postgresql.org/docs/9.4/static/libpq.html]

这是由 PostgreSQL 官方提供的 C 封装类库。

libpqxx

Home:[http://pqxx.org/development/libpqxx/]

这是由 PostgreSQL 官方提供的 C++ 封装类库。

代码示例#include
#include

void update(const std::string& name)
{
pqxx::connection conn(“dbname=company user=accounting”);
pqxx::work txn(conn);

pqxx::result r = txn.exec(“SELECT id FROM Employee WHERE name =” + name);
if(r.size() != 1)
{
std::cerr << "Expected 1 employee with name " << name << ", " << "but found " << r.size() << std::endl; return 1; } int employee_id = r[0][0].as();
std::cout << "Updating employee #" << employee_id << std::endl; txn.exec( "UPDATE EMPLOYEE SET salary = salary + 1 " "WHERE id = " + txn.quote(employee_id) ); txn.commit();}

=== 9.2.4 Oracle 封装库 ===

OCILIB

Home:[http://vrogier.github.io/ocilib/]

这是一个跨平台的 C 开源库。如其名,它封装了 Oracle 官方的 OCI(Oracle Call Interface)。

它同时还提供了 C++ 的 API。

代码示例#include
using namespace ocilib;

int main()
{
try
{
Environment::Initialize();
Connection conn(“db”, “user”, “password”);
Statement stmt(conn);
stmt.Execute(“select intcol, strcol from table”);

Resultset rs = stmt.GetResultset();
while(rs.Next())
{
std::cout << rs.Get(1) << " - " << rs.Get(2) << std::endl; } } catch(std::exception& err) { std::cout << err.what() << std::endl; } Environment::Cleanup(); return 0;}

=== 9.2.5 DB2 封装库 ===

=== 9.2.6 SQLite 封装库 ===

官方的 C API

Docs:[http://sqlite.org/c3ref/intro.html]

SQLite 前面已经介绍过。由于 SQLite 本身就是用 C 语言开发的,因此它直接提供了基于 C 的 API 接口。

代码示例#include
#include

static int callback(void* NotUsed, int argc, char** argv, char** azColName)
{
for(int i=0; i

POCO::Data::SQLite

Docs:[http://pocoproject.org/docs/package-SQLite.SQLite.html]

POCO 前面已经介绍过。它提供了 sqlite 的封装类

=== 9.2.7 Redis 封装库 ===

Hiredis

Home:[https://github.com/redis/hiredis]

这是 Redis 官方提供的 C 客户端,很轻量级,支持“异步 API”。

代码示例——结合 libev 进行异步调用#include
#include
#include
#include

#include
#include
#include

void getCallback(redisAsyncContext* context, void* r, void* privdata)
{
redisReply* reply = r;
if(reply == NULL)
{
return;
}
printf(“argv[%s]: %s\n”, (char*)privdata, reply->str);
redisAsyncDisconnect(context); /* Disconnect after receiv reply */
}

void connectCallback(const redisAsyncContext* context, int status)
{
if(status != REDIS_OK)
{
printf(“Error: %s\n”, context->errstr);
return;
}
printf(“Connected…\n”);
}

void disconnectCallback(const redisAsyncContext* context, int status)
{
if(status != REDIS_OK)
{
printf(“Error: %s\n”, context->errstr);
return;
}
printf(“Disconnected…\n”);
}

int main(int argc, char* argv[])
{
signal(SIGPIPE, SIG_IGN);

redisAsyncContext* context = redisAsyncConnect(“127.0.0.1”, 6379);
if(context->err)
{
printf(“Error: %s\n”, context->errstr);
return 1; /* Let *context leak for now… */
}

redisLibevAttach(EV_DEFAULT_ context);
redisAsyncSetConnectCallback(context, connectCallback);
redisAsyncSetDisconnectCallback(context, disconnectCallback);
redisAsyncCommand(context, NULL, NULL,
“SET key %b”, argv[argc-1], strlen(argv[argc-1]));
redisAsyncCommand(context, getCallback, (char*)”end-1″, “GET key”);
ev_loop(EV_DEFAULT_ 0);
return 0;
}

=== 9.2.8 MongoDB 封装库 ===

官方的 C API

Docs:[https://api.mongodb.org/c/current/]

MongoDB 前面已经介绍过。这是其官方提供的 API。

代码示例#include
#include
#include

int main()
{
mongoc_init();

mongoc_client_t* client = mongoc_client_new(“mongodb://localhost:27017/”);
mongoc_collection_t* collection = mongoc_client_get_collection(client, “test”, “test”);

bson_t* doc = bson_new();

bson_oid_t oid;
bson_oid_init(&oid, NULL);
BSON_APPEND_OID(doc, “_id”, &oid);
BSON_APPEND_UTF8(doc, “hello”, “world”);

bson_error_t error;
if(!mongoc_collection_insert(collection, MONGOC_INSERT_NONE, doc, NULL, &error))
{
printf(“%s\n”, error.message);
}

bson_destroy(doc);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);

return 0;
}

POCO::MongoDB

Docs:[http://pocoproject.org/docs/package-MongoDB.MongoDB.html]

POCO 前面已经介绍过。它提供了 MongoDB 的封装类

== 9.3 ODBC 相关 ==

unixODBC

Home:[http://www.unixodbc.org/]

Links:[https://en.wikipedia.org/wiki/UnixODBC Wikipedia]

诞生于1999年,实现了全套的 ODBC 架构,包括:驱动管理器、相关的 GUI 界面和命令行界面。支持多种操作系统。

Libodbc++

Home:[http://libodbcxx.sourceforge.net/]

如其名,这是专门封装 ODBC 的 C++ 类库,支持多种操作系统。它提供的 API 类似于 JDBC 的 API

POCO::Data::ODBC

Docs:[http://pocoproject.org/docs/package-ODBC.ODBC.html]

POCO 前面已经介绍过。它提供了操作 ODBC 的封装类。

== 9.4 ORM(Object-Relational Mapping) ==

ODB

Home:[http://www.codesynthesis.com/products/odb]

Links:[https://en.wikipedia.org/wiki/ODB_%28C%2B%2B%29 Wikipedia]

它的特色是:可以根据 C++ 类定义自动生成数据库的表结构。

为了获得高性能,它直接调用具体数据库的原生 API。支持的数据库包括:MySQL、PostgreSQL、Oracle、SQL Server、SQLite

代码示例——声明一个可持久化的类// 通过预处理语句“#pragma”来进行某些定制

#pragma db object table(“people”)
class person
{
public:
// ……

private:
friend class odb::access;
person();

#pragma db id auto
unsigned long id_;

string first_;
string last_;

#pragma type(“INT UNSIGNED”)
unsigned short age_;
};

代码示例——查询typedef odb::query query;
typedef odb::result result;

transaction trans(db.begin());
result r(db.query(query::last == “Doe” && query::age < 30));for(result::iterator i(r.begin()); i!=r.end(); ++i){ cout << "Hello, " << i->first() << endl;}trans.commit();

hiberlite

Home:[https://github.com/paulftw/hiberlite]

专门提供给 Sqlite 的 ORM 封装库。基于 C++ 开发,其 API 采用类似 Boost.Serialization 的风格。

—-

= 10 Web =

== 10.1 HTTP Server ==

Apache HTTP Server

Home:[https://httpd.apache.org/]

Links:[https://en.wikipedia.org/wiki/Apache_HTTP_Server Wikipedia] [https://zh.wikipedia.org/wiki/Apache_HTTP_Server 维基百科]

大名鼎鼎的 Apache,诞生于1995年,采用 C 和 C++ 开发。长期作为 Web Server 市场份额的老大。

Nginx

Home:[http://nginx.org/]

Links:[https://en.wikipedia.org/wiki/Nginx Wikipedia] [https://zh.wikipedia.org/wiki/Nginx 维基百科]

Web Server 的后起之秀,诞生于2002年,采用 C 语言开发。其市场份额如今排名第二。

POCO::Net

Docs:[http://pocoproject.org/docs/package-Net.HTTPServer.html]

POCO 前面已经介绍过。它提供了 HTTP Server 的封装类

Dlib::server_http

Docs:[http://dlib.net/network.html#server_http]

Dlib 前面已经介绍过。它提供了一个简单的 HTTP Server 的类(server_http)。

== 10.2 HTTP Client ==

cURL & libcurl

Docs:[http://curl.haxx.se/libcurl/c/]

libcurl 前面已经介绍过。它提供了【完整的】HTTP 协议支持。另,HTTP 2.0 标准刚出来不久,它就已经支持了。

代码示例——HTTP POST#include
#include

int main()
{
curl_global_init(CURL_GLOBAL_ALL);

CURL* curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, “http://post.example.com/foo.cgi”);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, “name=daniel&project=curl”);

CURLcode res = curl_easy_perform(curl);
if(res != CURLE_OK) // Check for errors
{
fprintf(stderr, “curl_easy_perform() failed: %s\n”,
curl_easy_strerror(res));
}
curl_easy_cleanup(curl); // always cleanup
}

curl_global_cleanup();
return 0;
}

POCO::Net

Docs:[http://pocoproject.org/docs/package-Net.HTTPClient.html]

POCO 前面已经介绍过。它提供了 HTTP Client 的封装类。

== 10.3 浏览器引擎 ==

WebKit

Home:[https://www.webkit.org/]

Links:[https://en.wikipedia.org/wiki/WebKit Wikipedia] [https://zh.wikipedia.org/wiki/WebKit 维基百科]

它是很多浏览器使用的渲染引擎,基于 C++ 开发。

Gecko

Home:[https://developer.mozilla.org/]

Links:[https://en.wikipedia.org/wiki/Gecko_(software) Wikipedia] [https://zh.wikipedia.org/wiki/Gecko 维基百科]

它是 Firefox 的渲染引擎,基于 C++ 开发,由 Mozilla 社区维护。


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明整理好的C/C++ 开源库及示例代码
喜欢 (0)

您必须 登录 才能发表评论!

加载中……