首页 > 解决方案 > 如何修复 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文件中才能编译没有错误?

标签: c++mongodbcmake

解决方案


推荐阅读