Search code examples
c++mongodbcmakeclionmongo-cxx-driver

Clion mongodb dependency setup


I am getting the following error:

--Configuring incomplete, errors occurred!
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
See also "C:/Users/GyuriX/.CLion2016.1/system/cmake/generated/Projects-33418280/33418280/Debug/CMakeFiles/CMakeOutput.log".
Please set them or make sure they are set and tested correctly in the CMake files:
BSON_LIBRARY
    linked by target "Projects" in directory D:/DEV/C/Projects
MONGODB_LIBRARY
    linked by target "Projects" in directory D:/DEV/C/Projects

Makefile:443: recipe for target 'cmake_check_build_system' failed
mingw32-make.exe: *** [cmake_check_build_system] Error 1

When I try to compile just a simple Hello World program using the following CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(Projects)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)

include_directories(C:/mongo-c-driver/lib)

find_library(MONGODB_LIBRARY mongoc-1.0)
find_library(BSON_LIBRARY bson-1.0)

add_executable(Projects ${SOURCE_FILES})
target_link_libraries(Projects ${MONGODB_LIBRARY} ${BSON_LIBRARY})

I have compiled the required libraries based on what the mongodb wiki said, so I have the library files: The files I have compiled

So the question is that what am I doing wrong and what should I do to be able to use the mongodb in CLion.


Solution

  • Error message means that find_library fails to find requested libraries.

    As you install libraries into non-default directories (according to the screenshot, it is C:\mongo-c-driver\bin), you need to hint for find_library() where it should search.

    One possible way is to set CMake variable CMAKE_LIBRARY_PATH:

    set(CMAKE_LIBRARY_PATH "C:\\mongo-c-driver\\bin")
    # Now find_library should be able to find libraries
    find_library(MONGODB_LIBRARY mongoc-1.0)
    find_library(BSON_LIBRARY bson-1.0)
    

    There are others way for hint to find_library, see its documentation about search algorithm it uses.