1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
#include "foo.hpp"
#include <iostream>
#include <link.h>
#include <string>

typedef Foo *FooConstructor();

int
main(int argc, char **argv)
{
    if (argc!=2) {
        std::cerr << "usage: " << argv[0] << " <shared-lib>" << std::endl;
        return 1;
    }

    void* handle = dlopen(argv[1], RTLD_LAZY | RTLD_LOCAL);
    if (!handle) {
        return 2;
    }
    FooConstructor *constructor = (FooConstructor*) dlsym(handle, "construct");
    if (!constructor) {
        dlclose(handle);
        return 3;
    }
    Foo *foo = constructor();

    foo->msg();
}