Я пытаюсь протестировать драйвер mongodb c ++ в Ubuntu 16.04.
Драйвер установлен в ${Devfolder}/sdk/mongodb/
, а тест - в ${Devfolder}/testMongoDb/
.
Код был скомпилирован и протестированиспользуя:
export PKG_CONFIG_PATH=${Devfolder}/sdk/mongodb/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${Devfolder}/sdk/mongodb/lib
c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)
Затем я хочу использовать драйвер mongodb в моем проекте CMake.
ugitho@ugitho:projects$ tree -L 3
.
├── sdk
│ └── mongodb
│ ├── bin
│ ├── include
│ ├── lib
│ └── share
├── testMongo
│ ├── a.out
│ ├── build
│ │ ├── CMakeCache.txt
│ │ ├── CMakeFiles
│ │ ├── cmake_install.cmake
│ │ ├── cmongodb
│ │ └── Makefile
│ ├── CMakeLists.txt
│ ├── test
│ └── test.cpp
Но я получил следующую ошибку:
ugitho@ugitho:build$ cmake ..
LIBMONGOCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/mongocxx/v_noabi;/home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBMONGOCXX_LIBRARIES = LIBMONGOCXX_LIBRARY_PATH-NOTFOUND;LIBBSONCXX_LIBRARY_PATH-NOTFOUND
LIBBSONCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBBSONCXX_LIBRARIES = LIBBSONCXX_LIBRARY_PATH-NOTFOUND
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
LIBBSONCXX_LIBRARY_PATH
linked by target "cmongodb" in directory
/home/ugitho/NGUYENKHAC/projects/testMongoDb
linked by target "cmongodb" in directory
/home/ugitho/NGUYENKHAC/projects/testMongoDb
LIBMONGOCXX_LIBRARY_PATH
linked by target "cmongodb" in directory
/home/ugitho/NGUYENKHAC/projects/testMongoDb
-- Configuring incomplete, errors occurred!
See also "/home/ugitho/NGUYENKHAC/projects/testMongoDb/build/CMakeFiles/CMakeOutput.log".
отредактировано *:
Вот CMakeLists.txt
и test.cpp
.
CMakeLists.txt :
cmake_minimum_required(VERSION 3.2)
project(cmongodb)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_PREFIX_PATH /home/ugitho/NGUYENKHAC/projects/sdk/mongodb)
#find_package(libmongocxx REQUIRED)
#find_package(libbsoncxx REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBMONGOCXX REQUIRED libmongocxx)
pkg_check_modules(LIBBSONCXX REQUIRED libbsoncxx)
message("LIBMONGOCXX_INCLUDE_DIRS = ${LIBMONGOCXX_INCLUDE_DIRS}")
message("LIBMONGOCXX_LIBRARIES = ${LIBMONGOCXX_LIBRARIES}")
message("LIBBSONCXX_INCLUDE_DIRS = ${LIBBSONCXX_INCLUDE_DIRS}")
message("LIBBSONCXX_LIBRARIES = ${LIBBSONCXX_LIBRARIES}")
set(COMMON_LIBRARIES ${LIBMONGOCXX_LIBRARIES} ${LIBBSONCXX_LIBRARIES})
set(SOURCE_FILES test.cpp)
add_executable(cmongodb ${SOURCE_FILES})
target_include_directories(cmongodb PUBLIC ${LIBMONGOCXX_INCLUDE_DIRS})
target_include_directories(cmongodb PUBLIC ${LIBBSONCXX_INCLUDE_DIRS})
target_link_libraries(cmongodb ${COMMON_LIBRARIES})
test.cpp :
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char **) {
std::cout << "1" << std::endl;
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;
}
return 0;
}
У вас есть идеи о том, что я пропустил здесь?
Обновления:
с новым CMakeLists.txt, больше ошибок cmake больше нет.
ugitho@ugitho:build$ cmake ..
-- Checking for module 'libmongocxx'
-- Found libmongocxx, version 3.4.0
-- Checking for module 'libbsoncxx'
-- Found libbsoncxx, version 3.4.0
CMAKE_PREFIX_PATH = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/shared
LIBMONGOCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/mongocxx/v_noabi;/home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBMONGOCXX_LIBRARIES = mongocxx;bsoncxx
LIBBSONCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBBSONCXX_LIBRARIES = bsoncxx
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ugitho/NGUYENKHAC/projects/testMongoDb/build
[1]+ Done gedit /home/ugitho/NGUYENKHAC/projects/testMongoDb/build/CMakeFiles/CMakeOutput.log
Ошибки теперь на уровне изготовления:
ugitho@ugitho:build$ make
Scanning dependencies of target cmongodb
[ 50%] Building CXX object CMakeFiles/cmongodb.dir/test.cpp.o
[100%] Linking CXX executable cmongodb
/opt/llvm/x86_64/6.0.0.g631a/bin/ld: error: unable to find library -lmongocxx
/opt/llvm/x86_64/6.0.0.g631a/bin/ld: error: unable to find library -lbsoncxx
/opt/llvm/x86_64/6.0.0.g631a/bin/ld: error: unable to find library -lbsoncxx
collect2: error: ld returned 1 exit status
make[2]: *** [cmongodb] Error 1
make[1]: *** [CMakeFiles/cmongodb.dir/all] Error 2
make: *** [all] Error 2
ОБНОВЛЕНИЕ *** 27/12/2018
cmake_minimum_required(VERSION 3.2)
project(cmongodb)
set(CMAKE_CXX_STANDARD 14)
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
set(CMAKE_PREFIX_PATH ${PARENT_DIR}/sdk/mongodb)
message("CMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH}")
find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
set(MongoDb_INCLUDE_DIR ${PARENT_DIR}/sdk/mongodb/include)
include_directories(${MongoDb_INCLUDE_DIR}/mongocxx/v_noabi
${MongoDb_INCLUDE_DIR}/bsoncxx/v_noabi)
set(SOURCE_FILES test.cpp)
add_executable(cmongodb ${SOURCE_FILES})
target_link_libraries(cmongodb ${LIBMONGOCXX_LIBRARIES}
${LIBBSONCXX_LIBRARIES})
Мне удалось скомпилировать с помощью этогоCMakeLists.txt.
Новая конфигурация проекта будет в ближайшее время.{GTest & Boost & MongoDb C ++ Driver}
Спасибо !!