how to install mongodb drivers for c++ in ubuntu?

2020-07-26 14:39发布

问题:

I want to develop a fairly simple application using c++ for mongoDB and I follow their tutorials : http://www.mongodb.org/pages/viewpage.action?pageId=133415

and for installing driver I followed this one : https://groups.google.com/forum/?fromgroups=#!msg/mongodb-user/-mPG7MDJgm8/nZSiN42DJWIJ (Waitman Gobble/5 jun answer)

but yet when I try to compile a simple application I will get following error :

fatal error: client/dbclient.h: No such file or directory

I'm pretty sure the problem is MongoDB c++ driver hasn't installed yet.

How can I install it properly?

回答1:

If you download the driver source code from here,

Unpack and unzip

tar xzf mongodb-linux-x86_64-v2.0-latest.tgz 

Then cd into the directory.

cd mongo-cxx-driver-v2.0/

Then use scons to build

scons

and install

sudo scons install

Then to compile code shown in the tutorial you need to also specify the /usr/local/include/mongo directory as a include file search path.

sudo  g++ tutorial.cpp -I/usr/local/include/mongo -lmongoclient 
-lboost_thread -lboost_filesystem -lboost_program_options -o tutorial

Then to run it you will need to edit the /etc/ld.so.conf file

sudo vi /etc/ld.so.conf 

and add

/usr/local/lib

Then run

sudo ldconfig

and run the tutorial

$ ./tutorial 
connected ok

As an alternative to editing the ld.so.config file you can use the LD_LIBRARY_PATH environment variable. So you would do

export LD_LIBRARY_PATH=/usr/local/lib
$ ./tutorial 
connected ok


回答2:

In Ubuntu packages for development are separate from packages for general use.

In order to make use of the mongodb header files and clientlibraries, you need to sudo apt-get install mongodb-dev libmongo-client-dev - this adds the headers that will allow you to #include the relevant header files.

This assumes that you've already installed the libmongo-client and mongodb packages, which contains the client library, although they should be installed when you install the -dev packages.



标签: c++ mongodb