Using the mongodb cxx driver in a cmake c++ project

install mongodb c++ driver ubuntu
mongodb c++ tutorial
mongodb c driver installation
mongodb with cpp
mongodb c++ centos
installing the mongocxx driver
mongodb qt
dbsoncxx_poly_use_mnmlstc

I am new to cmake and I want to connect to a mongodb using c++ and the latest mongodb cxx driver. I managed to compile and install the driver but now I stuck with using it in my cmake project.

I installed the mongodb driver with the default settings, so it's located under /usr/local/lib/include/mongocxx/v_noabi/mongocxx.

In my cmake file i manged to get the includes resolved with:

include_directories(/usr/local/lib/include/mongocxx/v_noabi /usr/local/lib/include/bsoncxx/v_noabi)

but I don't know how to actually link the driver libs to my executable. Could any one please help me?

You shouldn't need to do it that way. You can and should find the C++11 driver via either CMake's find_package or via pkg_check_modules subsystems. The C++11 driver supports both.

Installing the mongocxx driver, Building mongo-cxx-driver using CMake ExternalProject_Add. c++, mongodb, cmake, mongo-cxx-driver. asked by Pejman on 12:20AM - 07 Aug� The example above assumes: * Boost is found in C:\local\boost_1_60_0. * libmongoc is found in C:\mongo-c-driver. * mongocxx is to be installed into C:\mongo-cxx-driver. For building with Visual Studio 2017 (without a C++17 polyfill), it is necessary to configure with an additional option, /Zc:__cplusplus to opt into the correct definition of

I had done it linking all what is linked on mongocxx driver docs and using CMake 'find_package':

find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
include_directories(${LIBMONGOCXX_INCLUDE_DIR})
include_directories(${LIBBSONCXX_INCLUDE_DIR})
include_directories("/usr/local/include/mongocxx/v_noabi")
include_directories("/usr/local/include/bsoncxx/v_noabi")
include_directories("/usr/local/include/libmongoc-1.0")
include_directories("/usr/local/include/libbson-1.0")
include_directories("/usr/local/lib")

add_executable(YOUR_PROJECT ${SOURCE_FILES})

target_link_libraries(YOUR_PROJECT ${LIBMONGOCXX_LIBRARIES})
target_link_libraries(YOUR_PROJECT ${LIBBSONCXX_LIBRARIES})

[CXX-1901] Building C++ driver from master fails to find cmake , Contribute to mongodb/mongo-cxx-driver development by creating an account on GitHub. should only be used when making contributions back to the driver, as it is not stable for use in production. MongoDB C++11 Driver r3.2.1 Latest C++ 91.4% � CMake 3.7% � Python 2.9% � Shell 1.4% � Perl 0.3% � Batchfile 0.3%. We should have examples on the wiki of how to write a CMake project that locates the C++11 driver. It should document using both the pkg-config and the CMake FindModules mechanism. Attachments

This work for me:

cmake_minimum_required(VERSION 3.10)
project(cmongodb)

set(CMAKE_CXX_STANDARD 14)

find_package(libbsoncxx-static REQUIRED)
find_package(libmongocxx-static REQUIRED)

include_directories(${LIBMONGOCXX_STATIC_LIBRARIES})
include_directories(${LIBBSONCXX_STATIC_LIBRARIES})
include_directories(${LIBMONGOCXX_STATIC_INCLUDE_DIRS})
include_directories(${LIBBSONCXX_STATIC_INCLUDE_DIRS})

message(STATUS "${LIBMONGOCXX_STATIC_LIBRARIES}")
message(STATUS "${LIBMONGOCXX_STATIC_INCLUDE_DIRS}")

message(STATUS "${LIBBSONCXX_STATIC_LIBRARIES}")
message(STATUS "${LIBBSONCXX_STATIC_INCLUDE_DIRS}")

add_executable(cmongodb main.cpp mymongo.cpp mymongo.h)

target_link_libraries(cmongodb ${LIBMONGOCXX_STATIC_LIBRARIES} ${LIBBSONCXX_STATIC_LIBRARIES})

Here I install mongo db in prefix static. Commonly, you will romve all

static and _STATIC

Problems trying to compile MongoCxx - Drivers & ODMs, An example of how to compile MongoDB cxx driver using CMake. Download boost c++ 1.70 libraries and unzip to "C:/boost_1_70_0". Create a build folder:. Our team is stuck on a problem with connecting MongoDB CXX driver to QtCreator 5.10. We have builded and compiled mongo-c-driver and mongo-cxx-driver with CMake and msbuild (VS 2017 compiler). We created test project in Visual Studio added needed includes, libs, dependencies (also we copied dll files to debug folder) and it worked, we have

Steps for setting up the MongoDB C++ driver in CLion on macOS Mojave:
  1. brew install mongo-cxx-driver (if you run into any error saying that something is missing, then brew install it)

  2. Open CLion and create a file for the hello world code provided by the MongoDB documentation:

    #include <iostream>
    
    #include <bsoncxx/builder/stream/document.hpp>
    #include <bsoncxx/json.hpp>
    
    #include <mongocxx/client.hpp>
    #include <mongocxx/instance.hpp>
    
    int main(int, char**) {
        mongocxx::instance inst{};
        mongocxx::client conn{mongocxx::uri{}};
    
        bsoncxx::builder::stream::document document{};
    
        auto collection = conn["testdb"]["testcollection"];
        document << "hello" << "world";
    
        collection.insert_one(document.view());
        auto cursor = collection.find({});
    
        for (auto&& doc : cursor) {
            std::cout << bsoncxx::to_json(doc) << std::endl;
        }
    }
  1. Let CLion know where it can find the driver files by setting CMakeLists.txt to:
# replace PROJECT_NAME by the name of your clion project

cmake_minimum_required(VERSION 3.15)
project(PROJECT_NAME)

set(CMAKE_CXX_STANDARD 14)

add_executable(PROJECT_NAME main.cpp)

find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
include_directories(${LIBMONGOCXX_INCLUDE_DIR})
include_directories(${LIBBSONCXX_INCLUDE_DIR})
include_directories("/usr/local/include/mongocxx/v_noabi")
include_directories("/usr/local/include/bsoncxx/v_noabi")
include_directories("/usr/local/include/libmongoc-1.0")
include_directories("/usr/local/include/libbson-1.0")
include_directories("/usr/local/lib")


target_link_libraries(PROJECT_NAME ${LIBMONGOCXX_LIBRARIES})
target_link_libraries(PROJECT_NAME ${LIBBSONCXX_LIBRARIES})

Takes 5 minutes, no C driver, no running into weird issues while trying to build the drivers.

mongodb/mongo-cxx-driver: C++ Driver for MongoDB, I successfully built the latest MongoDB C driver version from github, and I tried to use a similar approach for the c++ driver. Can you please� I am new to cmake and I want to connect to a mongodb using c++ and the latest mongodb cxx driver. I managed to compile and install the driver but now I stuck with using it in my cmake project. I

zviederi/building-mongo-cxx-driver-using-cmake: An , I am new to cmake and I want to connect to a mongodb using c++ and the latest mongodb cxx driver. I managed to compile and install the driver but now I stuck� The source folder for the mongocxx driver has a folder called "cmake" which contains FindLibBSON.cmake and FindLibMongoC.cmake files. When I build mangrove, I need to specify -DCMAKE_MODULE_PATH=/path/to/mongo-cxx-driver/cmake/ to the cmake command.

Mongo DB C++ Driver build with Cmake, Here is an example of how to build mongo cxx drivers using CMake I modified the c file and also the files read by the c file in the new folder. (I did not change� Please set them or make sure they are set and tested correctly in the CMake files: BSON (ADVANCED) linked by target "mongoc_shared" in directory C:/mongo-c-driver-1.3.5 linked by target "mongoc_static" in directory C:/mongo-c-driver-1.3.5

Using the mongodb cxx driver in a cmake c++ project, One advantage of installing libmongoc with a package manager is that its Build files have been written to: /home/user/mongo-c-driver-x.y.z/cmake-build. See the MongoDB C++ Driver Manual and the Driver Installation Instructions for more details on downloading, installing, and using this driver. NOTE : The mongocxx 3.2.x series does not promise API or ABI stability across patch releases.

Comments
  • @paddy_89 Happy to hear you got it working. It is considered polite to upvote answers that are helpful, and accept the best answer.
  • @paddy_89 Would you please accept this answer as correct so that other people viewing the question will know that the answer is correct?
  • thank you sooo much, and it helps me a lot. I am using macOS and installed the mongo-cxx-driver using homebrew. according to the configures (/usr/local/Cellar/mongo-cxx-driver/3.1.1/lib/cmake/libmongocxx-3.1.1/libmongocxx-config.cmake ), it setted the include dirs and lib dirs as follow: set_and_check(LIBMONGOCXX_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/include/mongocxx/v_noabi")
  • my configuration is INCLUDE_DIRECTORIES( ${LIBBSONCXX_INCLUDE_DIRS} ${LIBMONGOCXX_INCLUDE_DIR}) and no need to add the following 2 absolute paths. hmm, no offence, this is just wish to help more dudes if it has some information
  • i mean LIBBSONCXX_INCLUDE_DIR vs LIBBSONCXX_INCLUDE_DIR[S]
  • Indeed, I use the include directory variable with the "S" at the end include_directories(${LIBMONGOCXX_INCLUDE_DIRS})
  • @datatype_void apologize for my late response. you may also append those libraries to COMMON_LIBRARIES