之前写过mongodb数据库的安装,现在要用C++连接数据库。
在网上看到几个C++连接数据库的例子,借用一下:
#include<iostream>
using namespace std;
#include <iostream>
#include "client/dbclient.h"
using namespace mongo;
using namespace std;
void run()
{
DBClientConnection c;
c.connect("localhost");
}
int main()
{
try {
run();
cout << "connected ok" << endl;
} catch( DBException &e ) {
cout << "caught " << e.what() << endl;
}
return 0;
}
建立一个main.cpp的测试文件,来测试是否能够连接成功。
编译g++ main.cpp -I /usr/include/mongo/ /usr/lib/libmongoclient.a -lboost_thread -lboost_filesystem -lboost_program_options,出现如下:
/usr/bin/ld: cannot find -lboost_thread
/usr/bin/ld: cannot find -lboost_filesystem
/usr/bin/ld: cannot find -lboost_program_options
找不到bo