Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 911
paddy_89 Avatar asked May 13 '16 12:05

paddy_89


3 Answers

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})
like image 103
Washington Santos Avatar answered Nov 03 '22 04:11

Washington Santos


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.

like image 25
acm Avatar answered Nov 03 '22 05:11

acm


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

like image 41
Manh Vu Avatar answered Nov 03 '22 05:11

Manh Vu