Created
November 21, 2014 22:16
-
-
Save kolodziej/71a7a5c40fa5147db8d8 to your computer and use it in GitHub Desktop.
Tutorial 0 for C++ modular application
This file contains 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
#include <dlfcn.h> | |
#include <iostream> | |
int main(int argc, char ** argv) | |
{ | |
if (argc == 1) | |
{ | |
std::cerr << "Usage: " << argv[0] << " modules...\n"; | |
return 1; | |
} | |
for (int i = 1; i < argc; ++i) | |
{ | |
void* shared_library = dlopen(argv[i], RTLD_LAZY); | |
void (*module)() = reinterpret_cast<void (*)()>(dlsym(shared_library, "module")); | |
if (module) | |
{ | |
module(); | |
dlclose(shared_library); | |
} else | |
{ | |
std::cerr << "Error while loading: " << argv[i] << "\n"; | |
} | |
} | |
return 0; | |
} |
This file contains 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
#include <iostream> | |
extern "C" void module() | |
{ | |
std::cout << "Other module function!\n"; | |
} |
This file contains 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
#include <iostream> | |
extern "C" void module() | |
{ | |
std::cout << "Start module function!\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment