0
votes

i am new to cmake and also to mongocxx. I have installed the mongocxx using the instructions given on the site http://mongocxx.org/mongocxx-v3/. My installatio is fine. Now I am trying to connect with mongodb using cmake in my project. When i write the following cmakelists.txt file

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})

it gives me following error :

CMake Error at CMakeLists.txt:8 (find_package):
By not providing "Findlibmongocxx.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"libmongocxx", but CMake did not find one.

 Could not find a package configuration file provided by "libmongocxx" with
any of the following names:

libmongocxxConfig.cmake
libmongocxx-config.cmake

Add the installation prefix of "libmongocxx" to CMAKE_PREFIX_PATH or set
 "libmongocxx_DIR" to a directory containing one of the above files.  If
 "libmongocxx" provides a separate development package or SDK, be sure it
  has been installed.


  -- Configuring incomplete, errors occurred!
  See also "/home/fedora/testmongo/build/CMakeFiles/CMakeOutput.log".

I also tried to run my cpp file with this method given on above website.

code of my cpp file is

  #include <cstdint>
  #include <iostream>
  #include <vector>
  #include <bsoncxx/json.hpp>
  #include <mongocxx/client.hpp>
  #include <mongocxx/stdx.hpp>
  #include <mongocxx/uri.hpp>
  #include <mongocxx/instance.hpp>
  #include <bsoncxx/builder/stream/helpers.hpp>
  #include <bsoncxx/builder/stream/document.hpp>
  #include <bsoncxx/builder/stream/array.hpp>


   using bsoncxx::builder::stream::close_array;
   using bsoncxx::builder::stream::close_document;
   using bsoncxx::builder::stream::document;
   using bsoncxx::builder::stream::finalize;
   using bsoncxx::builder::stream::open_array;
   using bsoncxx::builder::stream::open_document;
   mongocxx::instance instance{}; // This should be done only once.

   int main() {
   std::cout << "Hello, World!" << std::endl;


    mongocxx::client conn{ mongocxx::uri{ "mongodb://localhost:27017" } };
    auto coll = conn["test"]["coll"];
    std::vector<bsoncxx::document::value> documents;
    for(int i = 0; i < 100; i++) {
       documents.push_back(bsoncxx::builder::stream::document{} << "i" << i 
     << finalize);
       }
    coll.insert_many(documents);

     return 0;
     }

  

and im trying to compile it with this command. its not giving any error c++ --std=c++11 .cpp -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/libmongoc-1.0
-I/usr/local/include/bsoncxx/v_noabi -I/usr/local/include/libbson-1.0
-L/usr/local/lib -lmongocxx -lbsoncxx It complies without any errors and also generate output file a.out, but i don't know how to run that output file. its not running with

  ./a.out

gives following error ./a.out: error while loading shared libraries: libmongocxx.so._noabi: cannot open shared object file: No such file or directory """" This is the exact error i got what should i do to correct this error or run the below executable file?

2
The fact that your executable (a.out) is not run most likely means problem with your source file (main.cpp). You need to add code from this file into the question post for we being able to help you. - Tsyvarev
Please check it now. i don't know how to run a.out. Should it not run simple by ./a.out? - Sunny123
Assuming you have a.out file in the current directory, ./a.out should run the program. Probably, the error message is printed by your program. Show (add to the question post) the error message exactly. See e.g. that question on meta why exact error message is important. - Tsyvarev
"""./a.out: error while loading shared libraries: libmongocxx.so._noabi: cannot open shared object file: No such file or directory """" This is the exact error i got - Sunny123

2 Answers

0
votes
     export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/mongo-cxx-driver/lib64

These step resolved my problem

0
votes

Use this as your CMakeLists.txt. Change $YOUR_PROJECT_NAME with your project's name.

cmake_minimum_required(VERSION 3.5)

if(POLICY CMP0025)
    cmake_policy(SET CMP0025 NEW)
endif()

project($YOUR_PROJECT_NAME CXX)

if(NOT DEFINED CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_STANDARD 11)
endif()

set(CMAKE_CXX_EXTENSIONS OFF)

if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
endif()

find_package(mongocxx REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

# Visual Studio pre 2017 requires boost polyfill.
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND CMAKE_CXX_STANDARD LESS 17)
  find_package(Boost 1.56.0 REQUIRED)
  if (CMAKE_VERSION VERSION_LESS 3.15.0)
    target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS})
  else()
    target_link_libraries(${PROJECT_NAME} PRIVATE Boost::boost)
  endif()
endif()

target_link_libraries(${PROJECT_NAME}
  PRIVATE mongo::mongocxx_shared
)

add_custom_target(run
    COMMAND ${PROJECT_NAME}
    DEPENDS ${PROJECT_NAME}
    WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
)
    
get_target_property(LIBMONGOCXX_DEFINITIONS mongo::mongocxx_shared INTERFACE_COMPILE_DEFINITIONS)
list(FIND LIBMONGOCXX_DEFINITIONS "BSONCXX_STATIC" LIST_IDX)
if (${LIST_IDX} GREATER -1)
    message(FATAL_ERROR "Expected BSONCXX_STATIC to not be defined")
endif()
list(FIND LIBMONGOCXX_DEFINITIONS "MONGOCXX_STATIC" LIST_IDX)
if (${LIST_IDX} GREATER -1)
    message(FATAL_ERROR "Expected MONGOCXX_STATIC to not be defined")
endif()