Created
November 1, 2016 13:06
-
-
Save erayarslan/2c82cc3de8905b9d751d72bc0cf184e0 to your computer and use it in GitHub Desktop.
dlopen-Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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