首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用示例代码测试mongocxx c++驱动程序

用示例代码测试mongocxx c++驱动程序
EN

Stack Overflow用户
提问于 2017-10-15 21:55:16
回答 2查看 1.6K关注 0票数 0

我试图在C++示例代码中将mongdb与mongocxx驱动程序连接起来。我添加了额外的包含、库和依赖项。当我构建它时,它会显示以下错误。

这是完整的代码

代码语言:javascript
复制
#include "stdafx.h"
#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/stdx.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() {
    std::cout << "Hello, World!" << std::endl;

    mongocxx::instance instance{}; // This should be done only once.
    mongocxx::uri uri("mongodb://localhost:27017");
    mongocxx::client client(uri);

    return 0;
}
thi

S是错误输出。

代码语言:javascript
复制
Severity    Code    Description Project File    Line    Suppression State
Error   LNK1120 10 unresolved externals MongoCTest1 C:\Users\Nuwanst\source\repos\MongoCTest1\x64\Release\MongoCTest1.exe   1   
Error (active)  E0145   member "bsoncxx::v_noabi::types::b_array::type_id" may not be initialized   MongoCTest1 c:\mongo-cxx-driver\include\bsoncxx\v_noabi\bsoncxx\types.hpp   181 
Error (active)  E0145   member "bsoncxx::v_noabi::types::b_binary::type_id" may not be initialized  MongoCTest1 c:\mongo-cxx-driver\include\bsoncxx\v_noabi\bsoncxx\types.hpp   206 
Error (active)  E0145   member "bsoncxx::v_noabi::types::b_bool::type_id" may not be initialized    MongoCTest1 c:\mongo-cxx-driver\include\bsoncxx\v_noabi\bsoncxx\types.hpp   264 
Error (active)  E0145   member "bsoncxx::v_noabi::types::b_code::type_id" may not be initialized    MongoCTest1 c:\mongo-cxx-driver\include\bsoncxx\v_noabi\bsoncxx\types.hpp   421 
Error (active)  E0145   member C:\Users\Nuwanst\source\repos\MongoCTest1\MongoCTest1\MongoCTest1.obj    1   
Error   LNK2001 unresolved external symbol "__declspec(dllimport) public: __cdecl mongocxx::v_noabi::instance::instance(void)" (__imp_??0instance@v_noabi@mongocxx@@QEAA@XZ)    MongoCTest1 C:\Users\Nuwanst\source\repos\MongoCTest1\MongoCTest1\MongoCTest1.obj   1   
Error   LNK2001 unresolved external symbol "__declspec(dllimport) public: __cdecl mongocxx::v_noabi::instance::~instance(void)" (__imp_??1instance@v_noabi@mongocxx@@QEAA@XZ)   MongoCTest1 C:\Users\Nuwanst\source\repos\MongoCTest1\MongoCTest1\MongoCTest1.obj   1   
Error   LNK2001 unresolved external symbol "__declspec(dllimport) public: __cdecl mongocxx::v_noabi::options::client::client(void)" (__imp_??0client@options@v_noabi@mongocxx@@QEAA@XZ) MongoCTest1 C:\Users\Nuwanst\source\repos\MongoCTest1\MongoCTest1\MongoCTest1.obj   1   
Error   LNK2001 unresolved external symbol "__declspec(dllimport) public: __cdecl mongocxx::v_noabi::options::client::~client(void)" (__imp_??1client@options@v_noabi@mongocxx@@QEAA@XZ)    MongoCTest1 C:\Users\Nuwanst\source\repos\MongoCTest1\MongoCTest1\MongoCTest1.obj   1   
Error   LNK2001 unresolved external symbol "__declspec(dllimport) public: __cdecl mongocxx::v_noabi::uri::uri(class bsoncxx::v_noabi::string::view_or_value)" (__imp_??0uri@v_noabi@mongocxx@@QEAA@Vview_or_value@string@1bsoncxx@@@Z)  MongoCTest1 C:\Users\Nuwanst\source\repos\MongoCTest1\MongoCTest1\MongoCTest1.obj   1   
Error   LNK2001 unresolved external symbol "__declspec(dllimport) public: __cdecl mongocxx::v_noabi::uri::~uri(void)" (__imp_??1uri@v_noabi@mongocxx@@QEAA@XZ)  MongoCTest1 C:\Users\Nuwanst\source\repos\MongoCTest1\MongoCTest1\MongoCTest1.obj   1   
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-01 14:38:44

上面写的是先初始化你的驱动程序。如果您正确地编译了您的驱动程序,这些代码必须是工作的。

代码语言:javascript
复制
   #include <cstdlib>
   #include <iostream>
   #include <bsoncxx/builder/stream/document.hpp>
   #include <bsoncxx/json.hpp>
   #include <mongocxx/client.hpp>
   #include <mongocxx/instance.hpp>
   #include <mongocxx/uri.hpp>


   using bsoncxx::builder::stream::close_document;
   using bsoncxx::builder::stream::document;
   using bsoncxx::builder::stream::finalize;
   using bsoncxx::builder::stream::open_document;
   mongocxx::instance instance{};// don't put inside main 

int main() {
mongocxx::client conn{ mongocxx::uri{ "mongodb://localhost:27017" } };
auto coll = conn["test"]["coll"];
bsoncxx::builder::stream::document document{};
document << "Data" << "hello";
coll.insert_one(document.view());

return 0;
}
票数 3
EN

Stack Overflow用户

发布于 2020-01-05 10:28:09

根据这个示例,驱动程序是在main内部启动的。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46760393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档