Much better but not perfect version of dynamic compile and dynamic load code. DynamicLib class and rudimentar example.
This commit is contained in:
parent
951448f6b2
commit
120743e59c
@ -1,7 +1,12 @@
|
||||
## Dynamic Code
|
||||
|
||||
```
|
||||
man nm
|
||||
|
||||
cd memgraph/dc
|
||||
cd example
|
||||
clang++ -std=c++1y mysql.cpp -o ../tmp/mysql.so -shared -fPIC
|
||||
clang++ -std=c++1y memsql.cpp -o ../tmp/memsql.so -shared -fPIC
|
||||
cd ..
|
||||
clang++ -std=c++1y test.cpp -o test.out -ldl
|
||||
```
|
||||
|
65
dc/dynamic_lib.hpp
Normal file
65
dc/dynamic_lib.hpp
Normal file
@ -0,0 +1,65 @@
|
||||
#ifndef MEMGRAPH_DL_DYNAMIC_LIB_HPP
|
||||
#define MEMGRAPH_DL_DYNAMIC_LIB_HPP
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <dlfcn.h>
|
||||
|
||||
template<typename T>
|
||||
class DynamicLib
|
||||
{
|
||||
public:
|
||||
typename T::produce produce_method;
|
||||
typename T::destruct destruct_method;
|
||||
|
||||
DynamicLib(const std::string& lib_path) :
|
||||
lib_path(lib_path)
|
||||
{
|
||||
}
|
||||
|
||||
void load()
|
||||
{
|
||||
load_lib();
|
||||
load_produce_func();
|
||||
load_destruct_func();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string lib_path;
|
||||
void *dynamic_lib;
|
||||
|
||||
void load_lib()
|
||||
{
|
||||
dynamic_lib = dlopen(lib_path.c_str(), RTLD_LAZY);
|
||||
if (!dynamic_lib) {
|
||||
throw std::runtime_error(dlerror());
|
||||
}
|
||||
dlerror();
|
||||
}
|
||||
|
||||
void load_produce_func()
|
||||
{
|
||||
produce_method = (typename T::produce) dlsym(
|
||||
dynamic_lib,
|
||||
T::produce_name.c_str()
|
||||
);
|
||||
const char* dlsym_error = dlerror();
|
||||
if (dlsym_error) {
|
||||
throw std::runtime_error(dlsym_error);
|
||||
}
|
||||
}
|
||||
|
||||
void load_destruct_func()
|
||||
{
|
||||
destruct_method = (typename T::destruct) dlsym(
|
||||
dynamic_lib,
|
||||
T::destruct_name.c_str()
|
||||
);
|
||||
const char *dlsym_error = dlerror();
|
||||
if (dlsym_error) {
|
||||
throw std::runtime_error(dlsym_error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
@ -8,8 +8,14 @@ using namespace std;
|
||||
class db
|
||||
{
|
||||
public:
|
||||
virtual void name() const;
|
||||
virtual void type() const;
|
||||
// If virtual methods don't have = 0 the compiler
|
||||
// won't create appropriate _ZTI symbol inside
|
||||
// the .so lib. That will lead to undefined symbol
|
||||
// error while the library is loading.
|
||||
//
|
||||
// TODO: why?
|
||||
virtual void name() const = 0;
|
||||
virtual void type() const = 0;
|
||||
virtual ~db() {}
|
||||
};
|
||||
|
||||
|
68
dc/test.cpp
68
dc/test.cpp
@ -4,12 +4,11 @@
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
#include <cstdlib>
|
||||
#include <dlfcn.h>
|
||||
#include "example/db.hpp"
|
||||
#include "dynamic_lib.hpp"
|
||||
|
||||
// TODO: clang++ file.cpp -o file.so -shared -fPIC
|
||||
|
||||
using namespace std;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
void write(const std::string& path, const std::string& content)
|
||||
{
|
||||
@ -34,39 +33,24 @@ std::string prints(const Args&... args)
|
||||
return join(strings, " ");
|
||||
}
|
||||
|
||||
db* database(const std::string& lib_path, const std::string& factory_method)
|
||||
// dependent on specific dynamic code
|
||||
// "configuration" of DynamicLib
|
||||
// DynamicLib<MemgraphDynamicLib>
|
||||
class MemgraphDynamicLib
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
public:
|
||||
const static std::string produce_name;
|
||||
const static std::string destruct_name;
|
||||
typedef produce_t produce;
|
||||
typedef destruct_t destruct;
|
||||
};
|
||||
const std::string MemgraphDynamicLib::produce_name = "produce";
|
||||
const std::string MemgraphDynamicLib::destruct_name = "destruct";
|
||||
|
||||
int main()
|
||||
{
|
||||
// -- compile example
|
||||
|
||||
// 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; }";
|
||||
@ -74,11 +58,27 @@ int main()
|
||||
// write(tmp_file_path, for_compile);
|
||||
// string test_command = prints("clang++", tmp_file_path, "-o", "test.out");
|
||||
// system(test_command.c_str());
|
||||
|
||||
// -- end compile example
|
||||
|
||||
db *mysql = database("./tmp/mysql.so", "produce");
|
||||
// -- load example
|
||||
using db_lib = DynamicLib<MemgraphDynamicLib>;
|
||||
|
||||
db_lib mysql_db("./tmp/mysql.so");
|
||||
mysql_db.load();
|
||||
auto mysql = mysql_db.produce_method();
|
||||
if (mysql) {
|
||||
mysql->name();
|
||||
}
|
||||
mysql_db.destruct_method(mysql);
|
||||
|
||||
db_lib memsql_db("./tmp/memsql.so");
|
||||
memsql_db.load();
|
||||
auto memsql = memsql_db.produce_method();
|
||||
if (memsql) {
|
||||
memsql->name();
|
||||
}
|
||||
memsql_db.destruct_method(memsql);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user