According to what I understood from your reply I have attached the code below. Please help !
interface.h contains
Code:
#ifndef __INTERFACE_H
#define __INTERFACE_H
#include <iostream>
class interface
{
public:
virtual int get_result()=0;
};
typedef interface* create_t();
#endif
and mongoquery.cpp contains
Code:
#include "interface.h"
#include <client/dbclient.h>
#include <iostream>
#define HOST "localhost"
class mongoquery:public interface
{
public:
virtual int get_result()
{
mongo::DBClientConnection c;
try
{
c.connect(HOST);
}catch(mongo::DBException e)
{
std::cerr<<"Error interacting with DB"<<e.what();
return 0;
}
int count=c.count("test.rules");
return count;
}
};
extern "C" interface* create()
{
return new mongoquery;
}
I am making mongoquery.cpp as
Code:
g++ -fpic -shared -static -I /home/mayank/Desktop/mongo-cxx-driver-v1.8/mongo mongoquery.cpp -o libmongoquery.so -lmongoclient -lboost_filesystem -lboost_thread-mt -lboost_program_option
and the test file
Code:
#include <iostream>
#include <dlfcn.h>
#include "interface.h"
using namespace std;
int main()
{
void *lib_handle;
interface * (*fn)();
lib_handle=dlopen("./libmongoquery.so",RTLD_NOW);
if(!lib_handle)
{
cerr<<"Error"<<dlerror();
return 1;
}
create_t* cro=(create_t*) dlsym(lib_handle,"create");
const char* dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Cannot load symbol create: " << dlsym_error << '\n';
return 1;
}
interface *obj=cro();
cout<<obj->get_result()<<endl;
dlclose(lib_handle);
return 0;
}