Skip to content

Instantly share code, notes, and snippets.

@erayarslan
Created November 1, 2016 13:06
Show Gist options
  • Select an option

  • Save erayarslan/2c82cc3de8905b9d751d72bc0cf184e0 to your computer and use it in GitHub Desktop.

Select an option

Save erayarslan/2c82cc3de8905b9d751d72bc0cf184e0 to your computer and use it in GitHub Desktop.
dlopen-Example
/**
* @module do.cpp
* @author eray arslan
* @date 06.2015
*
* dylib : g++ -dynamiclib -o do.dylib do.cpp
* so : g++ -bundle -o do.so do.cpp
*
*/
#include <iostream>
#include <unistd.h>
using std::cout;
extern "C" void doIt() {
cout << getpid();
}
/**
* @module main.cpp
* @author eray arslan
* @date 06.2015
*
* g++ main.cpp
* ./a.out
*
*/
#include <iostream>
#include <dlfcn.h>
const char * f = "doIt";
const char * l = "do.dylib"; // or do.so
using namespace std;
int main() {
void* handle = dlopen(l, RTLD_LAZY);
if (!handle) { return 1; }
typedef void (*doIt_f)();
dlerror();
doIt_f doIt = (doIt_f) dlsym(handle, f);
const char *dlsym_error = dlerror();
if (dlsym_error) {
dlclose(handle);
return 1;
}
doIt();
dlclose(handle);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment