Skip to content

Instantly share code, notes, and snippets.

@kolodziej
Created Nov 21, 2014
Embed
What would you like to do?
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