为什么 mongo C++驱动程序给我编译错误

Why is the mongo C++ driver giving me compilation errors?

本文关键字:编译 错误 驱动程序 mongo C++ 为什么      更新时间:2023-10-16

我已经直接从github安装了mongo,使用

sudo scons --full install

并具有以下示例源文件

#include <cstdlib>
#include <iostream>
#include <mongo/client/dbclient.h>
void run() {
  mongo::DBClientConnection c;
  c.connect("localhost");
}
int main() {
  try {
    run();
    std::cout << "connected ok" << std::endl;
  } catch( const mongo::DBException &e ) {
    std::cout << "caught " << e.what() << std::endl;
  }
  return EXIT_SUCCESS;
}

当我跑步时

g++ tutorial.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_filesystem 
-lboost_program_options -lboost_system -o tutorial

我得到了错误

In file included from /usr/local/include/mongo/util/net/hostandport.h:21:0,
                 from /usr/local/include/mongo/util/net/message.h:24,
                 from /usr/local/include/mongo/client/dbclientinterface.h:30,
                 from /usr/local/include/mongo/client/connpool.h:23,
                 from /usr/local/include/mongo/client/dbclient.h:32,
                 from tutorial.cpp:3:
/usr/local/include/mongo/db/server_options.h:34:51: fatal error: 
mongo/util/options_parser/environment.h: No such file or directory
compilation terminated.

我查看了/usr/local/include/mongo/util,但 options_parser 文件夹不在那里。

在关注MongoDB网站上的文章后,我自己也遇到了同样的错误。我最终做的是将标头从下载目录复制到我的包含目录。即

    sudo cp -R ~/Downloads/mongo-master/src/mongo/util/options_parser /usr/local/include/mongo/util/

其中 mongo-master 是从 MongoDB 的 GitHub 中提取的目录的名称。希望这对您有所帮助。