File tree Expand file tree Collapse file tree 11 files changed +538
-0
lines changed
Expand file tree Collapse file tree 11 files changed +538
-0
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 1+ ALL = dlopen testlib1.so testlib2.so
2+
3+ all : $(ALL )
4+
5+ clean :
6+ rm -f $(ALL )
7+
8+ dlopen : dlopen.c
9+ $(CC ) dlopen.c -o $@
10+
11+ testlib1.so : testlib1.c
12+ $(CC ) -shared testlib1.c -o testlib1.so
13+
14+ testlib2.so : testlib2.c
15+ $(CC ) -shared testlib2.c -o testlib2.so
16+
17+ test : all
18+ ./dlopen testlib1.so testa
19+ ./dlopen testlib1.so testb
20+ ./dlopen testlib2.so testa
21+ ./dlopen testlib2.so testb
Original file line number Diff line number Diff line change 1+ #include <dlfcn.h>
2+ #include <stdio.h>
3+
4+ int main (int argv , char * * argc )
5+ {
6+ char * * library_name ;
7+ void * i ;
8+ void (* test )();
9+
10+ if ( argv < 3 )
11+ { printf ("Usage: dlopen library_to_use.so function_to_call\n" ); return -1 ; }
12+
13+ if ( ! ( i = dlopen (argc [1 ], RTLD_GLOBAL | RTLD_LAZY ) ) )
14+ { printf ("Could not open library %s\n" , argc [1 ]); return -1 ; }
15+
16+ if ( ! ( library_name = dlsym (i , "libname" ) ) )
17+ { printf ("Could not find library name in library %s\n" , argc [1 ], argc [2 ]); return -1 ; }
18+
19+ printf ("Using library: %s\n" , * library_name );
20+
21+ if ( ! ( test = dlsym (i , argc [2 ]) ) )
22+ { printf ("Could not find symbol %s in library %s\n" , argc [1 ], argc [2 ]); return -1 ; }
23+
24+ test ();
25+ return 0 ;
26+ }
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+
3+ const char * libname = "Test 1" ;
4+
5+ void testa () { printf ("hello from lib1 [testlib1.c - testa()]\n" ); }
6+
7+ void testb () { printf ("hello from lib1 [testlib1.c - testb()]\n" ); }
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+
3+ const char * libname = "Test 2" ;
4+
5+ void testa () { printf ("hello from lib2 [testlib2.c - testa()]\n" ); }
6+
7+ void testb () { printf ("hello from lib2 [testlib2.c - testb()]\n" ); }
Original file line number Diff line number Diff line change 1+ ALL = dlopen foo.so bar.so baz.so
2+
3+ all : $(ALL )
4+
5+ clean :
6+ rm -f $(ALL )
7+
8+ dlopen : dlopen.cc
9+ $(CXX ) dlopen.cc -o $@
10+
11+ foo.so : foo.cc
12+ $(CXX ) -fPIC -shared foo.cc -o foo.so
13+
14+ bar.so : bar.cc
15+ $(CXX ) -fPIC -shared bar.cc -o bar.so
16+
17+ baz.so : baz.cc
18+ $(CXX ) -fPIC -shared baz.cc -o baz.so
19+
20+ test : $(ALL )
21+ ./dlopen foo.so
22+ ./dlopen bar.so
23+ ./dlopen baz.so
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include " plugin.h"
3+
4+ using namespace std ;
5+
6+ const char * classname = " bar Object" ;
7+
8+ class bar : public plugin
9+ {
10+ public:
11+ bar () { cout << " bar created [bar.cc - bar::bar()]" << endl; };
12+ ~bar () { cout << " bar destroyed [bar.cc - bar::~bar()]" << endl; };
13+ virtual void test () { cout << " bar tested [bar.cc - bar::test()]" << endl; };
14+ };
15+
16+
17+ class barfactory : public factory
18+ {
19+ public:
20+ plugin * makedyn () { cout << " making bar [bar.cc barfactory::makedyn()]" << endl; return new bar; }
21+ };
22+
23+ barfactory Factory;
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include " plugin.h"
3+
4+ using namespace std ;
5+
6+ const char * classname = " baz Object" ;
7+
8+ class baz : public plugin
9+ {
10+ public:
11+ baz () { cout << " baz created [baz.cc - baz::baz()]" << endl; };
12+ ~baz () { cout << " baz destroyed [baz.cc - baz::~baz()]" << endl; };
13+ virtual void test () { cout << " baz tested [baz.cc - baz::test()]" << endl; };
14+ };
15+
16+
17+ class bazfactory : public factory
18+ {
19+ public:
20+ plugin * makedyn () { cout << " making baz [baz.cc bazfactory::makedyn()]" << endl; return new baz; }
21+ };
22+
23+ bazfactory Factory;
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < dlfcn.h>
3+ #include " plugin.h"
4+
5+ using namespace std ;
6+
7+ factory * myFactory;
8+
9+ int main (int argv, char ** argc)
10+ {
11+ char ** name;
12+ void * handle;
13+ factory * myFactory;
14+ plugin * myPlugin;
15+
16+ if ( argv < 2 )
17+ { cout << " Usage: dlopen plugin_to_use.so" << endl; return -1 ; }
18+
19+ if ( ! ( handle = dlopen (argc[1 ], 0 ) ) )
20+ { cout << " Could not open library " << argc[1 ] << " : " << dlerror () << endl; return -1 ; }
21+
22+ if ( ! ( name = (char **)dlsym (handle, " classname" ) ) )
23+ { cout << " Could not find classname in " << argc[1 ] << endl; return -1 ; }
24+
25+ cout << " creating a \" " << *(name) << " \" ..." << endl;
26+
27+ if ( ! ( myFactory = (factory *)dlsym (handle, " Factory" ) ) )
28+ { cout << " Could not find Factory in " << argc[1 ] << endl; return -1 ; }
29+
30+ myPlugin = myFactory->makedyn ();
31+ myPlugin->test ();
32+ delete myPlugin;
33+
34+ dlclose (handle);
35+ return 0 ;
36+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include " plugin.h"
3+
4+ using namespace std ;
5+
6+ const char * classname = " foo Object" ;
7+
8+ class foo : public plugin
9+ {
10+ public:
11+ foo () { cout << " foo created [foo.cc - foo::foo()]" << endl; };
12+ ~foo () { cout << " foo destroyed [foo.cc - foo::~foo()]" << endl; };
13+ virtual void test () { cout << " foo tested [foo.cc - foo::test()]" << endl; };
14+ };
15+
16+
17+ class foofactory : factory
18+ {
19+ public:
20+ foo * makedyn () { cout << " making foo [foo.cc foofactory::makedyn()]" << endl; return new foo; }
21+ };
22+
23+ foofactory Factory;
You can’t perform that action at this time.
0 commit comments