How to install MongoDB C++ drivers for Ubuntu?

2020-06-06 05:40发布

I have downloaded the latest MongoDB C++ driver, http://downloads.mongodb.org/cxx-driver/mongodb-linux-x86_64-2.5.2.tgz. When I do 'scons', it builds just fine. Once the building is complete, I am getting the following:

ranlib build/libmongoclient.a
Install file: "build/libmongoclient.a" as "libmongoclient.a"
scons: done building targets.

It says it installs mongo, but I would like to install the lib and the headers in a proper place, like /usr/local. No matter what I try (scons install, with or without --prefix), it just doesn't want to install it in /usr/local. In fact, It says that the install target is up to date:

$sudo scons install --prefix=/usr/local
scons: Reading SConscript files ...
Checking for C++ library boost_thread-mt... (cached) yes
Checking for C++ library boost_filesystem-mt... (cached) yes
Checking for C++ library boost_system-mt... (cached) yes
Checking for sasl_version_info(0, 0, 0, 0, 0, 0) in C library sasl2... (cached) yes
Checking for C++ header file execinfo.h... (cached) yes
Checking whether backtrace is declared... (cached) yes
Checking whether backtrace_symbols is declared... (cached) yes
Checking whether backtrace_symbols_fd is declared... (cached) yes
scons: done reading SConscript files.
scons: Building targets ...
scons: `install' is up to date.
scons: done building targets.

Should i maybe use other parameters to install it? For the library, I can just copy it, but the headers would be more tedious to install manually.

3条回答
Anthone
2楼-- · 2020-06-06 05:59

Currently the best way to install the MongoDB C++ driver is to go to the official github repository and read the "Download and Compile" wiki page.

There are three branches of the repository (two currently offering a stable release stream). The first stable release series is named 26compat (MongoDB 2.6 compatible) which has the source code as extracted from the 2.6 release of MongoDB (along with minor modifications to make it independent). It is intended as a drop in replacement for existing code built around the C++ driver.

The other stable release series is named legacy and it is similar to the 26compat branch but contains some backward breaking improvements and many important improvements and fixes. It is the correct starting point for new projects. Existing projects using the old "server driver" or the 26compat driver should also aim to upgrade to the stable legacy release series.

If you are interested, you can read more about the changes here but in general you will have a better time using what we provide at the github repository now. While the C++ driver was technically available before via the server source it was not really intended to be used external to MongoDB. Now, the code we are providing at the official repository is intended to be used externally and supported by the C++ driver team.

We are also working on an entirely new driver built for C++11 on the master branch, but that is not yet production ready. We encourage you to experiment with it and provide feedback.

查看更多
家丑人穷心不美
3楼-- · 2020-06-06 06:00

EDIT: This posting gets a lot of traffic, but describes how to build the now-officially-out-of-date "Legacy C++" Driver. The Legacy C++ driver has been obsoleted by the new C++11 driver. The new C++11 driver should be the first choice when writing a client application in C++ that will communicate with MongoDB. Information on how to build the new C++11 driver is available here:

https://github.com/mongodb/mongo-cxx-driver/wiki/Quickstart-Guide-(New-Driver)

EDIT: Please note that this answer is obsolete. Tyler's answer below is the correct answer for all modern users of the C++ driver.

I recommend against trying to use the C++ driver tarball from the 2.5 release (or, for that matter, in the 2.4 release) to build the driver library.

The best way to build the C++ driver is from the complete mongodb source archive. There are several reasons for this, but the most important is that the tarball build offers only a very minimal build system. As you noticed, it has no 'install' targets. Another limitation is that the tarball build cannot produce a shared object, while the full sources can.

Here are some basic instructions on building the C++ driver from the primary mongodb sources:

  • Clone the mongodb sources from https://github.com/mongodb/mongo
  • Run scons --prefix=<path-to-install> --full --use-system-all install-mongoclient
  • If you want a shared library (supported in latest 2.4, and latest 2.5), add --sharedclient to the line above.
  • If you want a debug build of the library, pass either --dd (2.4), or --dbg=on (2.5 tip of trunk).

The --use-system-all flag is very important: using it inhibits building against the vendored in versions of things like boost and pcre that come with the primary mongodb sources. You do not want your library to build against those libraries, since the resulting library will not be appropriate to mix with applications built against the system versions of those libraries. This is particularly important when building with --sharedclient.

One challenge with the above is that when building with --use-system-all, the build system checks for the presence of various libraries on the system. Some of these libraries, like v8, may not be available. On 2.4, you must either install an applicable version of the library, or edit the SConstruct to inhibit the CheckLib calls that look for the library. On 2.5, this is not an issue as missing libraries only cause the build to fail if you try to build a target that depends on the missing library. Since building the C++ client driver only depends on a small number of libraries (like boost), it is much easier to get working.

I also recommend building with a test --prefix argument first to ensure that the installation actions are as you intend.

Hope this helps.

查看更多
霸刀☆藐视天下
4楼-- · 2020-06-06 06:08

Unfortunately the library isn't automatically installed. It is compiled so you can distribute the library with your application. Including the cpp or using the tarball file isn't recommended.

Please see the documentation at this link: http://docs.mongodb.org/ecosystem/tutorial/download-and-compile-cpp-driver/#download-and-compile-cpp-driver

Best, Charlie

查看更多
登录 后发表回答