NOSQL Mongo入门学习笔记 - C++连接Mongodb(三)
OS环境:
Centos 7.1 release
X86_64
编译环境:
G++ 4.8.3
已经成功搭建好了Mongodb,也初步在命令行中的查询与写入数据的基本方法,现在通过C++来连接Mongodb。
1.准备工作
(1).在官网中下载mongodb的C++ driver
官方文档说明:
https://github.com/mongodb/mongo-cxx-driver/wiki/Download-and-Compile-the-Legacy-Driver
值得注意的是,上方文档说明中的git仓库已经不可用,在github中可以搜索到,使用如下地址
https://github.com/mongodb/mongo-cxx-driver.git
clone下来:
git clone https://github.com/mongodb/mongo-cxx-driver.git
(2).安装构建工具
mongodb 使用scons工具来自动化构建mongodb driver for c++, 而我centos上没有预装这个工具,为了节省时间,我直接使用yum来安装
yum install scons。
(3).安装Mongodb驱动 / mongodb-c++开发中所必需的库。这就是复杂但是好用,庞大但是很富有精华的强大的Boost库。
为了节省时间,我也选用的是二进制包来安装,需要的话也可以选择源码编译。
yum install boost-devel
安装完成之后,当前在我的Centos上,头文件存在于/usr/include/boost , 动态库文件存在与/usr/lib64/ 下.
安装Boost库也是编译C++ mongodb驱动的必要步骤.
2.开始编译driver
(1).编译mongodb-cxx-driver
在上述准备工作都已做好的时候,就可以来编译驱动了。
cd mongo-cxx-driver , 直接 scons 命令,就开始自动编译构建驱动 [ 需要更改安装路径的可以选择 --prefix=$HOME/local/mongo ]选项,在编译的时候若系统中没有存在Boost开发库的话,会报错,提示需要安装boost开发库,若需要请先看第三步。确认无误之后,编译完成会构建出build目录,目录结构:
|-build
|-----install
|----------include
|----------lib
|-----linux2
|-----scons
在这里,我们需要的是install/include 与 install/lib文件。而且lib下只有一个库文件libmongodbclient.a
cd build
cp include ~/local/include
cp lib ~/local/lib
复制到我们常用开发的头文件与库文件目录中。
我在编译的时候没有遇到过报错。这样,驱动已成功编译完成
3.使用C++来连接Mongodb
/**
* Mongodb connect for c++
* author kk
* 2015/06/17
*/ #include <iostream>
#include "mongo/client/dbclient.h" using namespace mongo;
using namespace std; void run(){
DBClientConnection c;
c.connect("localhost");
} int main(){
try{
run();
cout<<"connect ok"<<endl;
}catch(DBException &e){
cout<<"caught"<<e.what()<<endl;
}
return ;
}
单个文件就不使用makefile了,输入命令
g++ main.cpp -I ~/local/include/ ~/local/lib/libmongoclient.a -lboost_thread-mt -lboost_filesystem -lboost_program_options
在大多数教程中会提醒你这么来编译你的源代码,但是,这样会报错,详见错误
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
/usr/include/boost/regex/v4/basic_regex.hpp:382: undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::unwind_extra_block(bool)':
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:1117: undefined reference to `boost::re_detail::put_mem_block(void*)'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::cpp_regex_traits_implementation<char>::error_string(boost::regex_constants::error_type) const':
/usr/include/boost/regex/v4/cpp_regex_traits.hpp:447: undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `void boost::re_detail::raise_error<boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > >(boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::error_type)':
/usr/include/boost/regex/pattern_except.hpp:75: undefined reference to `boost::re_detail::raise_runtime_error(std::runtime_error const&)'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::cpp_regex_traits_implementation<char>::error_string(boost::regex_constants::error_type) const':
/usr/include/boost/regex/v4/cpp_regex_traits.hpp:445: undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `boost::cpp_regex_traits<char>::transform_primary(char const*, char const*) const':
/usr/include/boost/regex/v4/cpp_regex_traits.hpp:923: undefined reference to `boost::re_detail::cpp_regex_traits_implementation<char>::transform_primary(char const*, char const*) const'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `boost::cpp_regex_traits<char>::transform(char const*, char const*) const':
/usr/include/boost/regex/v4/cpp_regex_traits.hpp:919: undefined reference to `boost::re_detail::cpp_regex_traits_implementation<char>::transform(char const*, char const*) const'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::extend_stack()':
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:213: undefined reference to `boost::re_detail::get_mem_block()'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `save_state_init':
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:107: undefined reference to `boost::re_detail::get_mem_block()'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_imp()':
/usr/include/boost/regex/v4/perl_matcher_common.hpp:206: undefined reference to `boost::re_detail::verify_options(unsigned int, boost::regex_constants::_match_flags)'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `~save_state_init':
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:115: undefined reference to `boost::re_detail::put_mem_block(void*)'
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:115: undefined reference to `boost::re_detail::put_mem_block(void*)'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `perl_matcher':
/usr/include/boost/regex/v4/perl_matcher.hpp:374: undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
/home/kangkai/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_match()':
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:973: undefined reference to `boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > >::maybe_assign(boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > > const&)'
collect2: error: ld returned 1 exit status
对于向来喜欢编译来安装的我来说,这样的错误实在郁闷了一阵。导致这样的原因无非就是动态库链接错误,找不到声明与符号。根据提示,只需在编译时加上动态库就好了。
g++ main.cpp -I ~/local/include/ ~/local/lib/libmongoclient.a /usr/lib64/libboost_regex-mt.so -lboost_thread-mt -lboost_filesystem -lboost_program_options
OK,无输出即便就是正确。
./a.out
connect ok
4.结尾
到这里,C++连接Mongodb已经成功,只是需要Boost库略显庞大。
NOSQL Mongo入门学习笔记 - C++连接Mongodb(三)的更多相关文章
- NOSQL Mongo入门学习笔记 - MongoDB的安装(一)
手上的工作不是很忙,所以来学习学习很久就像接触的MongoDb,无奈前段时间工作时间都比较多.记录在这里供以后参考 环境: Centos 7 64位 开始: 1. 在官网下载Mongo : wget ...
- NOSQL Mongo入门学习笔记 - 数据的基本插入(二)
成功运行起来mongo之后,进入了命令行模式,mongo默认会选择test数据库 1. 使用db命令打印出来当前选定的数据库: > db test 2. 使用show dbs 命令可以打印出数据 ...
- MongoDB学习笔记一:MongoDB的下载和安装
MongoDB学习笔记一:MongoDB的下载和安装 趁着这几天比較空暇,准备学习一下MongoDB数据库.今天就简单的学习了一些MongoDB的下载和安装.并创建了存储MongoDB的数据仓库. 将 ...
- OpenCV入门学习笔记
OpenCV入门学习笔记 参照OpenCV中文论坛相关文档(http://www.opencv.org.cn/) 一.简介 OpenCV(Open Source Computer Vision),开源 ...
- dubbo入门学习笔记之入门demo(基于普通maven项目)
注:本笔记接dubbo入门学习笔记之环境准备继续记录; (四)开发服务提供者和消费者并让他们在启动时分别向注册中心注册和订阅服务 需求:订单服务中初始化订单功能需要调用用户服务的获取用户信息的接口(订 ...
- Sass简单、快速上手_Sass快速入门学习笔记总结
Sass是世界上最成熟.稳定和强大的专业级css扩展语言 ,除了Sass是css的一种预处理器语言,类似的语言还有Less,Stylus等. 这篇文章关于Sass快速入门学习笔记. 资源网站大全 ht ...
- MongoDB学习笔记:Python 操作MongoDB
MongoDB学习笔记:Python 操作MongoDB Pymongo 安装 安装pymongopip install pymongoPyMongo是驱动程序,使python程序能够使用Mong ...
- Hadoop入门学习笔记---part4
紧接着<Hadoop入门学习笔记---part3>中的继续了解如何用java在程序中操作HDFS. 众所周知,对文件的操作无非是创建,查看,下载,删除.下面我们就开始应用java程序进行操 ...
- Hadoop入门学习笔记---part3
2015年元旦,好好学习,天天向上.良好的开端是成功的一半,任何学习都不能中断,只有坚持才会出结果.继续学习Hadoop.冰冻三尺,非一日之寒! 经过Hadoop的伪分布集群环境的搭建,基本对Hado ...
随机推荐
- Servlet & JSP - UrlRewriteFilter
重写 URL 的好处有很多: 静态化页面,有利于搜索引擎收录. 隐藏真实的 URL,提高安全性. 当网站的结构发生变化时,无需要求用户修改书签. UrlRewriteFilter 的简单应用 1. M ...
- ActiveMQ(5.10.0) - Spring Support
Maven Dependency: <dependencies> <dependency> <groupId>org.apache.activemq</gro ...
- Jersey(1.19.1) - JSON Support
Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...
- 消息推送SignalR
一.什么是 SignalR ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of add ...
- JAVA之Switch语句
switch case语句是用来判断case后面的表达式是否与switch的表达式一致,符合的话就执行case语句,不符合则break跳出.而default是当没有符合case的条件下执行的(它不用b ...
- JIRA的常用选项
常用的一些选项有: 问题类型 Bug 测试过程维护过程发现影响系统运行的缺陷 New Feature 对系统提出的新功能 Task 需要完成的任务 Improvement 对现有系统功能的改 ...
- 20141015--for语句2
for语句,打印等腰三角形: 第一种方法:(使用for语句嵌套) 第二种方法:(定义string型变量) 以下是其他形状的等腰三角形: (穿插使用了for语句嵌套,定义string型)
- 引用类型之Function类型
Function类型 ECMAScript中最有意思的就是函数了,有意思的根源,在于函数实际上是对象.每个函数都是Function的实例,具有属性和方法.而重要的一点是,函数名,不过是指向函数的指针, ...
- 免费的HTML5连载来了《HTML5网页开发实例详解》连载(五)图解通过Fiddler加速开发
Fiddler是Windows底下最强大的请求代理调试工具,监控任何浏览器的HTTP/HTTPS流量,窜改客户端请求和服务器响应,解密HTTPS Web会话,图4.44为Fiddler原理示意图. 图 ...
- 百练_2945 拦截导弹(DP)
描述 某国为了防御敌国的导弹袭击,开发出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹来袭 ...