用mongodb构建一个c++项目

Posted

技术标签:

【中文标题】用mongodb构建一个c++项目【英文标题】:Build a c++ project with mongodb 【发布时间】:2018-12-08 19:07:05 【问题描述】:

我想将测量结果保存在 mongoDB 文档中,并在进一步的操作中使用这些数据。

我已按照 Joyo Waseem 在comment 中描述的步骤安装所需的驱动程序。 但是,我仍然在某些文件(alignment_of.hpp;is_nothrow_move_assignable.hpp;view_or_value.hpp;types.hpp;等)中收到了几条错误消息(具体为 82 条)。这些只是一些会产生错误的文件。

错误消息是:

Severity Code Line Description Error (active) E0243 36 class or struct definition is missing Error (active) E1574 29 static assertion failed with "Arguments to is_nothrow_move_constructible must be complete types" Error (active) E0070 31 incomplete type is not allowed Error (active) E1574 31 static assertion failed with "Arguments to is_nothrow_move_assignable must be complete types" Error (active) E0262 36 not a class or struct name Error (active) E1018 36 namespace "bsoncxx" has no member class "view_or_value" Error (active) E0070 41 incomplete type is not allowed Error (active) E0276 41 name followed by '::' must be a class or namespace name Error (active) E0260 48 explicit type is missing ('int' assumed) Error (active) E0135 48 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 48 expected a ';' Error (active) E0276 56 name followed by '::' must be a class or namespace name Error (active) E0260 63 explicit type is missing ('int' assumed) Error (active) E0135 63 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 63 expected a ';' Error (active) E0070 64 incomplete type is not allowed Error (active) E0276 72 name followed by '::' must be a class or namespace name Error (active) E0260 79 explicit type is missing ('int' assumed) Error (active) E0135 79 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 79 expected a ';' Error (active) E0145 86 member "bsoncxx::v_noabi::types::b_double::type_id" may not be initialized Error (active) E0276 87 name followed by '::' must be a class or namespace name Error (active) E0260 94 explicit type is missing ('int' assumed) Error (active) E0135 94 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 94 expected a ';' Error (active) E0276 102 name followed by '::' must be a class or namespace name Error (active) E0106 105 invalid type for a bit field Error (active) E0040 105 expected an identifier Error (active) E0260 109 explicit type is missing ('int' assumed) Error (active) E0135 109 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 109 expected a ';' Error (active) E0145 111 member "bsoncxx::v_noabi::types::b_utf8::type_id" may not be initialized Error (active) E0106 114 invalid type for a bit field Error (active) E0040 114 expected an identifier Error (active) E0260 124 explicit type is missing ('int' assumed) Error (active) E0135 124 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 124 expected a ';' Error (active) E0077 127 this declaration has no storage class or type specifier Error (active) E0135 127 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 127 expected a ';' Error (active) E0077 128 this declaration has no storage class or type specifier Error (active) E0135 128 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 128 expected a ';' Error (active) E0077 129 this declaration has no storage class or type specifier Error (active) E0135 129 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 129 expected a ';' Error (active) E0077 130 this declaration has no storage class or type specifier Error (active) E0135 130 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 130 expected a ';' Error (active) E0077 131 this declaration has no storage class or type specifier Error (active) E0135 131 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 131 expected a ';' Error (active) E0077 132 this declaration has no storage class or type specifier Error (active) E0135 132 namespace "mongocxx::v_noabi::stdx" has no member "optional" Error (active) E0065 132 expected a ';' Error (active) E0145 149 member "bsoncxx::v_noabi::types::b_document::type_id" may not be initialized Error (active) E0145 181 member "bsoncxx::v_noabi::types::b_array::type_id" may not be initialized Error (active) E0106 181 invalid type for a bit field Error (active) E0018 181 expected a ')' Error (active) E0106 190 invalid type for a bit field Error (active) E0040 190 expected an identifier Error (active) E0145 206 member "bsoncxx::v_noabi::types::b_binary::type_id" may not be initialized Error (active) E0145 230 member "bsoncxx::v_noabi::types::b_undefined::type_id" may not be initialized Error (active) E0145 246 member "bsoncxx::v_noabi::types::b_oid::type_id" may not be initialized Error (active) E0106 262 invalid type for a bit field Error (active) E0018 262 expected a ')' Error (active) E0145 264 member "bsoncxx::v_noabi::types::b_bool::type_id" may not be initialized Error (active) E0106 271 invalid type for a bit field Error (active) E0040 271 expected an identifier Error (active) E0145 289 member "bsoncxx::v_noabi::types::b_date::type_id" may not be initialized Error (active) E0145 350 member "bsoncxx::v_noabi::types::b_null::type_id" may not be initialized Error (active) E0145 366 member "bsoncxx::v_noabi::types::b_regex::type_id" may not be initialized Error (active) E0145 402 member "bsoncxx::v_noabi::types::b_dbpointer::type_id" may not be initialized Error (active) E0145 421 member "bsoncxx::v_noabi::types::b_code::type_id" may not be initialized Error (active) E0145 462 member "bsoncxx::v_noabi::types::b_symbol::type_id" may not be initialized Error (active) E0145 500 member "bsoncxx::v_noabi::types::b_codewscope::type_id" may not be initialized Error (active) E0145 533 member "bsoncxx::v_noabi::types::b_int32::type_id" may not be initialized Error (active) E0145 562 member "bsoncxx::v_noabi::types::b_timestamp::type_id" may not be initialized Error (active) E0145 581 member "bsoncxx::v_noabi::types::b_int64::type_id" may not be initialized Error (active) E0145 606 member "bsoncxx::v_noabi::types::b_decimal128::type_id" may not be initialized Error (active) E0145 637 member "bsoncxx::v_noabi::types::b_minkey::type_id" may not be initialized Error (active) E0145 653 member "bsoncxx::v_noabi::types::b_maxkey::type_id" may not be initialized

我不知道我是否做错了什么/跳过了一些被认为是不言而喻的标准,或者软件的某些部分是否不兼容。 只是为了确保,如果我使用 vcpkg 安装驱动程序,我还需要在属性中包含一些目录吗?例如附加的包含目录?使用vcpkg和cmake也有区别吗?

提前感谢您的帮助。

【问题讨论】:

【参考方案1】:

您的问题有点不清楚您的代码在做什么。我将在下面包含一个有关如何构建示例代码的示例。 vcpkg 是打包管理器,cmake 是构建工具。

在命令提示符下设置 vcpkg:

cd vcpkg
bootstrap-vcpkg.bat
vcpkg install mongo-cxx-driver

设置 Visual Studio 项目 添加源码

#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.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;

int main() 
    mongocxx::client mongo_client mongocxx::uri"mongodb://localhost:27017" ;
    return 0;

构建解决方案

1>------ Build started: Project: mongodb, Configuration: Debug Win32 ------
1>Source.cpp
1>mongodb.vcxproj -> D:\dev\cpptest\mongodb\Debug\mongodb.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

要运行 .exe,您需要将 D:\dev\GitHub\vcpkg\installed\x86-windows\bin 添加到路径中。这样.exe就可以找到mongocxx.dll

【讨论】:

非常感谢。您的描述对我帮助很大,现在我没有错误消息了。但是,如果我想调试它,它会因为未处理的异常而自动停止。在输出部分它说:Exception thrown at 0x0FB8AC5C (libmongoc-1.0.dll) in mongodb_test.exe: 0xC0000005: Access violation reading location 0x00000038. Unhandled exception thrown: read access violation. **Addend** was 0x38. 我做了一些研究,发现了一个建议删除存储在 data/db 文件夹中的文件的一面。这样做之后,我再次打开了我的项目,不,这表明我又遇到了 70 个错误。我查看了一些消息,但找不到任何解决方案。 请选择我的答案为正确。如果您在调试方面遇到问题,请提出一个新问题,我们也会进行调查。

以上是关于用mongodb构建一个c++项目的主要内容,如果未能解决你的问题,请参考以下文章

如何从我的项目访问我的MongoDB实例到我的xunit测试项目类?

MongoDB C++ 驱动程序稳定分支在 Qt 应用程序中崩溃

Elixir和mongodb聚合

在 mongodb 上构建多个权限组的最佳方法是啥?

使用 Express/MongoDB 构建收件箱系统

夺命雷公狗---node.js---19之项目的构建在node+express+mongo的博客项目4mongodb在项目中的基本引入