1

我正在尝试在我的机器上安装 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

任何帮助将不胜感激。

4

1 回答 1

4

该行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
于 2013-07-20T01:31:32.833 回答