MongoDB C Driver安装

这次就MongoDB来记录下安装C Driver过程。

Install MongoDB C Driver

我百度了一下,有关用C/C++调用MongoDB提供的接口的教程,貌似很少,我也没怎么Google,索性就看官网提供的文档算了,反正大部分都是代码。。。

MongoDB C Driver支持的平台如下

Supported Platforms
Archlinux
Debian 8.1
macOS 10.10
Microsoft Windows Server 2008
RHEL 7.0, 7.1, 7.2
SUSE 12
Ubuntu 12.04, 14.04, 16.04
Clang 3.4, 3.5, 3.7, 3.8
GCC 4.6, 4.8, 4.9, 5.3
MinGW-W64
Visual Studio 2010, 2013, 2015
x86, x86_64, ARM (aarch64), Power8 (ppc64le), zSeries (s390x)

从包管理工具安装

对于Debian或Ubuntu,可以直接从以下命令安装

apt-get install libmongoc-1.0-0

对于其他Linux系统,也是大同小异的。然而,我还是比较喜欢编译安装,感觉可控性更高些。

编译安装

OpenSSL is required for authentication or for SSL connections to MongoDB. Kerberos or LDAP support requires Cyrus SASL.

这一步我直接跳过。

注意 ,编译mongo-c-driver还需要 libbson 不过mongo-c-driver已经包含了该库源码

注意,–disable-automatic-init-and-cleanup 是官网推荐的参数,意思大概就是 禁止自动初始化和释放,这应该跟调用库有关,不过我没有加上

默认安装到 /usr/local

之后就可以 make + make install

Building from git

git clone https://github.com/mongodb/mongo-c-driver.git

这个方法我没怎么注意,有兴趣的可以自己尝试下

其他平台的构建

除了Linux,常用的Mac OS,Windows的安装方法这里就不在赘述了

注意

因为我是从源码编译安装的,在这过程中,也遇到了一些奇葩问题。
configure默认prefix为 /usr/local,libmongoc-1.0,libbson-1.0头文件安装到/usr/local/include,以及相应的链接库在/usr/local/lib。这似乎没有什么错误。但是当我用编写代码时发现我引用头文件

#include

时看到它里面其实包含

#include

而经过 locate bson.h,结果发现只存在 /usr/local/include/libbson-1.0/bson.h。。。这就是问题了

于是我可以cp /usr/local/include/libbson-1.0 目录下所有文件到 /usr/include

结尾

编译安装MongoDB C Driver算是简单了吧,与 MongoCXX 的安装方法相比,这的确算是简单了 😃

详细过程参阅 Installing the MongoDB C Driver

bye~


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!