Re: Anything new on run-time libraries loading?
On 11/02/2011 11:03 PM, Paavo Helde wrote:
Edek<edek.pienkowski@gmail.com> wrote in
news:j8r0rb$mqa$1@node2.news.atman.pl:
Why would you use nm? Why not make an extern C factory method
returning an object with virtual methods you can call (same example)?
The OP did not like extern "C" for some reason. I just clarified it is
technically possible to avoid it (though most probably not wise).
I guess it depends whether we mean "any library", or a plugin.
If the library is not a piece of unrelated software, one can write
a plugin without a single extern "C" and without dlsym. They
just need an API.
A working example (linux, gcc):
----------------------- api.h ------------------
#include <string>
#include <map>
using namespace std;
struct Exc {};
struct object
{
object () {};
virtual void hello () const =0;
virtual void throwExc () const = 0;
};
typedef map<string, object*> RegistryT;
-------------------- lib.cpp ------------------
#include "api.h"
#include <iostream>
using namespace std;
extern RegistryT registry;
namespace lib {
struct MyObject : public object
{
void hello () const { cerr << "MyObject" << endl; }
void throwExc () const { throw Exc() ; }
};
struct Registration
{
Registration () {
registry["lib"] = new MyObject();
}
};
static Registration reg;
} // namespace
--------------------- main.cpp ----------------
#include "api.h"
#include <dlfcn.h>
#include <iostream>
RegistryT registry;
int main()
{
dlopen("./lib.so", RTLD_NOW | RTLD_GLOBAL);
registry["lib"]->hello();
try {
registry["lib"]->throwExc();
} catch (Exc& e) {
cout << "caught Exc" << endl;
}
}
Edek