Skip to content

Instantly share code, notes, and snippets.

@kolodziej
Created November 21, 2014 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kolodziej/71a7a5c40fa5147db8d8 to your computer and use it in GitHub Desktop.
Save kolodziej/71a7a5c40fa5147db8d8 to your computer and use it in GitHub Desktop.
Tutorial 0 for C++ modular application
#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;
}
#include <iostream>
extern "C" void module()
{
std::cout << "Other module function!\n";
}
#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