5

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?

paddy_89
  • 298
  • 2
  • 5

4 Answers4

17

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})
  • 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") – Lumen Feb 12 '17 at 03:00
  • 1
    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 – Lumen Feb 12 '17 at 03:03
  • 2
    i mean LIBBSONCXX_INCLUDE_DIR vs LIBBSONCXX_INCLUDE_DIR[S] – Lumen Feb 12 '17 at 03:04
  • 4
    Indeed, I use the include directory variable with the "S" at the end include_directories(${LIBMONGOCXX_INCLUDE_DIRS}) – paceholder Jul 31 '17 at 21:47
  • Following these instructions I am getting missing symbols during linking :/ Still not sure what is wrong, installed via homebrew as @YuJing – datatype_void Sep 22 '17 at 02:12
  • Opened a new question if anyone has any insight: https://stackoverflow.com/questions/46356623/linking-errors-when-building-project-using-mongo-cxx-driver – datatype_void Sep 22 '17 at 04:03
  • 1
    @datatype_void apologize for my late response. you may also append those libraries to COMMON_LIBRARIES – Lumen Nov 08 '17 at 21:30
3

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.

acm
  • 12,183
  • 5
  • 39
  • 68
  • @paddy_89 Happy to hear you got it working. It is considered polite to upvote answers that are helpful, and accept the best answer. – acm May 16 '16 at 15:47
  • @paddy_89 Would you please accept this answer as correct so that other people viewing the question will know that the answer is correct? – acm May 17 '16 at 15:41
1

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

Manh Vu
  • 131
  • 1
  • 6
0

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.

Community
  • 1
  • 1
PlsWork
  • 1,958
  • 1
  • 19
  • 31