在 cmake C++ 项目中使用 mongodb cxx 驱动程序
Using the mongodb cxx driver in a cmake c++ project
我是 cmake 的新手,我想使用 c++ 和最新的 mongodb cxx 驱动程序连接到 mongodb。我设法编译并安装了驱动程序,但现在我坚持在我的 cmake 项目中使用它。
我使用默认设置安装了 mongodb 驱动程序,因此它位于 /usr/local/lib/include/mongocxx/v_noabi/mongocxx 下。
在我的 cmake 文件中,我设法通过以下方式解决了包含问题:
include_directories(/usr/local/lib/include/mongocxx/v_noabi /usr/local/lib/include/bsoncxx/v_noabi)
但我不知道如何实际 link 驱动程序库到我的可执行文件。谁能帮帮我吗?
你不应该那样做。您可以而且应该通过 CMake 的 find_package or via pkg_check_modules 子系统找到 C++11 驱动程序。 C++11 驱动程序支持两者。
我已经完成链接 mongocxx 驱动程序文档上链接的所有内容并使用 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})
这对我有用:
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})
这里我安装了 mongo db 前缀静态。通常,你会 romve all
static
和 _STATIC
在 macOS Mojave 上的 CLion 中设置 MongoDB C++ 驱动程序的步骤:
brew install mongo-cxx-driver
(如果你 运行 遇到任何错误说缺少某些东西,然后 brew install 它)
打开 CLion 并为 MongoDB 文档提供的 hello world 代码创建一个文件:
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
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;
}
}
- 通过将
CMakeLists.txt
设置为: 让 CLion 知道在哪里可以找到驱动程序文件
# replace PROJECT_NAME by the name of your clion project
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})
需要 5 分钟,没有 C 驱动程序,在尝试构建驱动程序时没有 运行遇到奇怪的问题。
我是 cmake 的新手,我想使用 c++ 和最新的 mongodb cxx 驱动程序连接到 mongodb。我设法编译并安装了驱动程序,但现在我坚持在我的 cmake 项目中使用它。
我使用默认设置安装了 mongodb 驱动程序,因此它位于 /usr/local/lib/include/mongocxx/v_noabi/mongocxx 下。
在我的 cmake 文件中,我设法通过以下方式解决了包含问题:
include_directories(/usr/local/lib/include/mongocxx/v_noabi /usr/local/lib/include/bsoncxx/v_noabi)
但我不知道如何实际 link 驱动程序库到我的可执行文件。谁能帮帮我吗?
你不应该那样做。您可以而且应该通过 CMake 的 find_package or via pkg_check_modules 子系统找到 C++11 驱动程序。 C++11 驱动程序支持两者。
我已经完成链接 mongocxx 驱动程序文档上链接的所有内容并使用 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})
这对我有用:
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})
这里我安装了 mongo db 前缀静态。通常,你会 romve all
static
和 _STATIC
在 macOS Mojave 上的 CLion 中设置 MongoDB C++ 驱动程序的步骤:
brew install mongo-cxx-driver
(如果你 运行 遇到任何错误说缺少某些东西,然后 brew install 它)打开 CLion 并为 MongoDB 文档提供的 hello world 代码创建一个文件:
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
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;
}
}
- 通过将
CMakeLists.txt
设置为: 让 CLion 知道在哪里可以找到驱动程序文件
# replace PROJECT_NAME by the name of your clion project
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})
需要 5 分钟,没有 C 驱动程序,在尝试构建驱动程序时没有 运行遇到奇怪的问题。