dynamic lib load work in progress :P
This commit is contained in:
parent
8e1c4b53b8
commit
951448f6b2
@ -1 +1,7 @@
|
||||
## Dynamic Code
|
||||
|
||||
```
|
||||
cd example
|
||||
clang++ -std=c++1y mysql.cpp -o ../tmp/mysql.so -shared -fPIC
|
||||
clang++ -std=c++1y test.cpp -o test.out -ldl
|
||||
```
|
||||
|
19
dc/example/db.hpp
Normal file
19
dc/example/db.hpp
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef MEMGRAPH_DL_EXAMPLE_DB_HPP
|
||||
#define MEMGRAPH_DL_EXAMPLE_DB_HPP
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class db
|
||||
{
|
||||
public:
|
||||
virtual void name() const;
|
||||
virtual void type() const;
|
||||
virtual ~db() {}
|
||||
};
|
||||
|
||||
typedef db* (*produce_t)();
|
||||
typedef void (*destruct_t)(db*);
|
||||
|
||||
#endif
|
28
dc/example/memsql.cpp
Normal file
28
dc/example/memsql.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include "db.hpp"
|
||||
|
||||
class memsql : public db
|
||||
{
|
||||
public:
|
||||
|
||||
void name() const override
|
||||
{
|
||||
cout << "MemSQL" << endl;
|
||||
}
|
||||
|
||||
void type() const override
|
||||
{
|
||||
cout << "InMemory" << endl;
|
||||
}
|
||||
|
||||
~memsql() {}
|
||||
};
|
||||
|
||||
extern "C" db* produce()
|
||||
{
|
||||
return new memsql();
|
||||
}
|
||||
|
||||
extern "C" void destruct(db* p)
|
||||
{
|
||||
delete p;
|
||||
}
|
28
dc/example/mysql.cpp
Normal file
28
dc/example/mysql.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include "db.hpp"
|
||||
|
||||
class mysql : public db
|
||||
{
|
||||
public:
|
||||
|
||||
virtual void name() const
|
||||
{
|
||||
cout << "MySQL" << endl;
|
||||
}
|
||||
|
||||
virtual void type() const
|
||||
{
|
||||
cout << "Relational" << endl;
|
||||
}
|
||||
|
||||
~mysql() {}
|
||||
};
|
||||
|
||||
extern "C" db* produce()
|
||||
{
|
||||
return new mysql();
|
||||
}
|
||||
|
||||
extern "C" void destruct(db* p)
|
||||
{
|
||||
delete p;
|
||||
}
|
28
dc/example/neo4j.cpp
Normal file
28
dc/example/neo4j.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include "db.hpp"
|
||||
|
||||
class neo4j : public db
|
||||
{
|
||||
public:
|
||||
|
||||
void name() const override
|
||||
{
|
||||
cout << "Neo4j" << endl;
|
||||
}
|
||||
|
||||
void type() const override
|
||||
{
|
||||
cout << "Graph" << endl;
|
||||
}
|
||||
|
||||
~neo4j() {}
|
||||
};
|
||||
|
||||
extern "C" db* produce()
|
||||
{
|
||||
return new neo4j();
|
||||
}
|
||||
|
||||
extern "C" void destruct(db* p)
|
||||
{
|
||||
delete p;
|
||||
}
|
28
dc/example/postgresql.cpp
Normal file
28
dc/example/postgresql.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include "db.hpp"
|
||||
|
||||
class postgresql : public db
|
||||
{
|
||||
public:
|
||||
|
||||
void name() const override
|
||||
{
|
||||
cout << "PostgreSQL" << endl;
|
||||
}
|
||||
|
||||
void type() const override
|
||||
{
|
||||
cout << "Relational" << endl;
|
||||
}
|
||||
|
||||
~postgresql() {}
|
||||
};
|
||||
|
||||
extern "C" db* produce()
|
||||
{
|
||||
return new postgresql();
|
||||
}
|
||||
|
||||
extern "C" void destruct(db* p)
|
||||
{
|
||||
delete p;
|
||||
}
|
50
dc/test.cpp
50
dc/test.cpp
@ -4,6 +4,8 @@
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
#include <cstdlib>
|
||||
#include <dlfcn.h>
|
||||
#include "example/db.hpp"
|
||||
|
||||
// TODO: clang++ file.cpp -o file.so -shared -fPIC
|
||||
|
||||
@ -32,15 +34,51 @@ std::string prints(const Args&... args)
|
||||
return join(strings, " ");
|
||||
}
|
||||
|
||||
db* database(const std::string& lib_path, const std::string& factory_method)
|
||||
{
|
||||
// load lib
|
||||
void* db_lib = dlopen(lib_path.c_str(), RTLD_LAZY);
|
||||
if (!db_lib) {
|
||||
cerr << "Cannot load library: " << dlerror() << '\n';
|
||||
return nullptr;
|
||||
}
|
||||
dlerror();
|
||||
|
||||
// load produce method
|
||||
produce_t produce_db = (produce_t) dlsym(db_lib, factory_method.c_str());
|
||||
const char* dlsym_error = dlerror();
|
||||
if (dlsym_error) {
|
||||
cerr << "Cannot load symbol create: " << dlsym_error << '\n';
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// load destroy method
|
||||
// destruct_t destruct_db = (destruct_t) dlsym(db_lib, "destruct");
|
||||
// dlsym_error = dlerror();
|
||||
// if (dlsym_error) {
|
||||
// cerr << "Cannot load symbol destroy: " << dlsym_error << '\n';
|
||||
// return nullptr;
|
||||
// }
|
||||
|
||||
db *instance = produce_db();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string tmp_file_path = "tmp/tmp.cpp";
|
||||
string tmp_so_path = "tmp/tmp.so";
|
||||
string for_compile = "#include <iostream>\nint main() { std::cout << \"test\" << std::endl; return 0; }";
|
||||
// string tmp_file_path = "tmp/tmp.cpp";
|
||||
// string tmp_so_path = "tmp/tmp.so";
|
||||
// string for_compile = "#include <iostream>\nint main() { std::cout << \"test\" << std::endl; return 0; }";
|
||||
|
||||
write(tmp_file_path, for_compile);
|
||||
string test_command = prints("clang++", tmp_file_path, "-o", "test.out");
|
||||
system(test_command.c_str());
|
||||
// write(tmp_file_path, for_compile);
|
||||
// string test_command = prints("clang++", tmp_file_path, "-o", "test.out");
|
||||
// system(test_command.c_str());
|
||||
|
||||
db *mysql = database("./tmp/mysql.so", "produce");
|
||||
if (mysql) {
|
||||
mysql->name();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user