1。安装C++版驱动之前,先安装C版的驱动,过程如下:
我的安装平台:ubuntu14.04 64位,其他平台可参考:http://api.mongodb.org/c/current/installing.html#installing-unix
(1) 安装PDK:
-
sudo apt-get install pkg-config libssl-dev libsasl2-dev
(2)下载安装包,并配置:
-
$ wget https://github.com/mongodb/mongo-c-driver/releases/download/1.3.0/mongo-c-driver-1.3.0.tar.gz
-
$ tar xzf mongo-c-driver-1.3.0.tar.gz
-
$ cd mongo-c-driver-1.3.0
-
$ ./configure
安装成功并显示如下内容:
-
libmongoc was configured with the following options:
-
-
Build configuration:
-
Enable debugging (slow) : no
-
Compile with debug symbols (slow) : no
-
Enable GCC build optimization : yes
-
Enable automatic binary hardening : yes
-
Code coverage support : no
-
Cross Compiling : no
-
Fast counters : no
-
SASL : sasl2
-
SSL : yes
-
Libbson : bundled
-
-
Documentation:
-
Generate man pages : no
-
Install man pages : no
(3)make并安装,需要几分钟的过程:
-
$ make
-
$ sudo make install
2。C++版的安装过程:
安装前需求:
系统:Linux 或 OSX 编译器:clang++ 3.4+ 或 g++ 4.9+ Cmake:CMake 3.1+
(1)安装新版的Cmake:
卸载旧版本:
-
sudo apt-get remove cmake
新版本下载:
解压安装:
-
tar -zxvf cmake-3.4.1.tar.gz
-
cd cmake-3
-
./bootstrap && make && make install
(2)下载安装C++驱动:
-
git clone -b master https://github.com/mongodb/mongo-cxx-driver
-
cd mongo-cxx-driver/build
-
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local PKG_CONFIG_PATH=CDRIVER_INSTALL_PATH/lib/pkgconfig ..
-
sudo make && sudo make install
3。解决问题
在测试C驱动的时候,编译链接生成可执行文件,例子是/mongo-c-driver-1.3.0/examples下面的example-client.c
执行如下命令:
-
gcc example-client.c -o example $(pkg-config --cflags --libs libmongoc-1.0)
生成后,运行时会有问题:
-
./example
-
./example: error while loading shared libraries: libmongoc-1.0.so.0: cannot open shared object file: No such file or directory
查看链接问题:
-
ldd example
-
linux-vdso.so.1 => (0x00007fffd09ca000)
-
libmongoc-1.0.so.0 => not found
-
libbson-1.0.so.0 => not found
-
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9c1a0c5000)
-
/lib64/ld-linux-x86-64.so.2 (0x00007f9c1a48a000)
可知相应的库文件没有找到。
测试解决办法,成功运行:
-
LD_LIBRARY_PATH=/usr/local/lib ./example
查看:
-
cat ld.so.conf
-
include /etc/ld.so.conf.d/*.conf
-
cd /etc/ld.so.conf.d/
-
fakeroot-x86_64-linux-gnu.conf x86_64-linux-gnu_EGL.conf
i686-linux-gnu.conf x86_64-linux-gnu_GL.conf
libc.conf zz_i386-biarch-compat.conf
x86_64-linux-gnu.conf
可知链接库没有mongoc.conf配置文件,因此创建,并在文件中加入mongoc库的路径:
-
cd /usr/local/lib/
-
ls
-
libbson-1.0.la libmongoc-1.0.la libmongocxx.a
-
libbson-1.0.so libmongoc-1.0.so libmongocxx.so
-
libbson-1.0.so.0 libmongoc-1.0.so.0 libmongocxx.so.0
-
libbson-1.0.so.0.0.0 libmongoc-1.0.so.0.0.0 libmongocxx.so.0.3.0
-
libbsoncxx.a libmongoc-priv.la pkgconfig
-
libbsoncxx.so libmongoc-priv.so python2.7
-
libbsoncxx.so.0 libmongoc-priv.so.0 python3.4
-
libbsoncxx.so.0.3.0 libmongoc-priv.so.0.0.0 site_ruby
-
-
cd /etc/ld.so.conf.d/
-
sudo vi mongoc.conf
然后在上面新建的mongoc.conf文件中加入,相应的库路径:/usr/local/lib/即可解决问题
参考链接:
pkg-config 用法:http://www.cppblog.com/colorful/archive/2012/05/05/173750.html
mongoC++安装指导:(New-Driver)
mongoC安装指导:http://api.mongodb.org/c/current/?_ga=1.224779610.177216698.1449303645
C 驱动使用实例:http://api.mongodb.org/c/current/tutorial.html#starting-mongod
阅读(6295) | 评论(0) | 转发(0) |