c++ - 如何修复 C++ 应用程序中缺少的 Mongo CXX 标头?(CMake)
问题描述
我已经安装了 Mongo C 和 Mongo CXX 来构建一个使用 Mongo 作为数据库的应用程序。我使用 CMake 编译应用程序。
我的CMakeLists.txt文件如下所示:
# CMake
CMAKE_MINIMUM_REQUIRED(VERSION 3.10.2)
PROJECT(Proto-buffer CXX)
SET(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
# Libraries
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")
# Executables
ADD_EXECUTABLE(${PROJECT_NAME} ./source/classes/connection/connection.cpp)
该应用程序如下所示:
#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
int main(const int argc, const char *argv[]) {
mongocxx::instance instance{};
mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};
return 0;
}
cmake ../.
当我在build文件夹中编译应用程序时,我收到错误:
CMakeFiles/Proto-buffer.dir/source/classes/connection/connection.cpp.o: In function `main':
connection.cpp:(.text+0x53): undefined reference to `mongocxx::v_noabi::uri::uri(bsoncxx::v_noabi::string::view_or_value)'
connection.cpp:(.text+0xa8): undefined reference to `mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::
v_noabi::options::client const&)'
connection.cpp:(.text+0xc6): undefined reference to `mongocxx::v_noabi::client::~client()'
connection.cpp:(.text+0xd5): undefined reference to `mongocxx::v_noabi::uri::~uri()'
connection.cpp:(.text+0x129): undefined reference to `mongocxx::v_noabi::uri::~uri()'
collect2: error: ld returned 1 exit status
CMakeFiles/Proto-buffer.dir/build.make:94: recipe for target 'Proto-buffer' failed
make[2]: *** [Proto-buffer] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Proto-buffer.dir/all' failed
make[1]: *** [CMakeFiles/Proto-buffer.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
我希望编译这个应用程序没有错误。我已经阅读了安装文档、 Mongo 的CMakeLists示例以及 Stack Over Flow 上的其他问题。
我需要将哪些头文件添加到 CPP 文件中,或者我需要将哪些库/标志添加到CMakeLists.txt文件中才能编译没有错误?
解决方案
推荐阅读
- xamarin.forms - Xamarin 离线位置 我们如何通过 GPS 获得离线位置?
- amcharts - amCharts,每条柱都有渐变色
- python - 如何从列表中的列表中删除某些 int?
- javascript - javascript中的冒泡排序 - 递归版本不起作用
- docker - 主机上禁用的互联网连接会破坏 docker 网络
- kubernetes - 微服务:与外部服务的连接状态?
- c - 有没有办法通过 C 中的多个变量对结构进行排序?
- android - Android:socket.io io.socket.engineio.client.EngineIOException:XHR 轮询错误
- java - 黄瓜 AmbiguousStepDefinitionsException Java
- angular - 我如何将一些数据库字段放在编辑文本字段上以进行编辑?