MongoC++驱动程序:Mongo/client/dbclient.h:没有这样的文件或目录

Mongo C++ Driver: mongo/client/dbclient.h: No such file or directory

本文关键字:文件 Mongo 驱动程序 client dbclient MongoC++      更新时间:2023-10-16

我正在尝试在我的机器上安装MongoDB C++驱动程序。我按照这里的指示进行了操作,一切似乎都安装成功了。尽管如此,我似乎无法将标题包括在内。这里有一个简单的测试程序:

#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++ app/tutorial.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_filesystem -lboost_program_options -lboost_system -o tutorial
app/tutorial.cpp:3:35: error: mongo/client/dbclient.h: No such file or directory
app/tutorial.cpp: In function ‘void run()’:
app/tutorial.cpp:6: error: ‘mongo’ has not been declared
app/tutorial.cpp:6: error: expected `;' before ‘c’
app/tutorial.cpp:7: error: ‘c’ was not declared in this scope
app/tutorial.cpp: In function ‘int main()’:
app/tutorial.cpp:14: error: ISO C++ forbids declaration of ‘mongo’ with no type
app/tutorial.cpp:14: error: expected `)' before ‘::’ token
app/tutorial.cpp:14: error: expected `{' before ‘::’ token
app/tutorial.cpp:14: error: ‘::DBException’ has not been declared
app/tutorial.cpp:14: error: ‘e’ was not declared in this scope
app/tutorial.cpp:14: error: expected `;' before ‘)’ token

如有任何帮助,我们将不胜感激。

app/tutorial.cpp:3:35: error: mongo/client/dbclient.h: No such file or directory表示g++很难找到已安装的标头。在您链接到的教程中,建议编译命令下方的框中显示

您可能需要使用-I和-L来指定mongo和boost头文件和库的位置。

我假设安装过程将头文件放在/usr/local/include中,将库(例如libmongoclient.a)放在/usr/local/lib中。然后,尝试调整编译命令以读取

g++ -I/usr/local/include -L/usr/local/lib -pthread -lmongoclient -lboost_thread-mt -lboost_filesystem -lboost_program_options -lboost_system app/tutorial.cpp -o tutorial