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?
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. - Tsyvareva.outfile in the current directory,./a.outshould 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