[cig-commits] r6650 - in cs/cigma/trunk/sandbox: . dlopen

luis at geodynamics.org luis at geodynamics.org
Tue Apr 24 11:18:26 PDT 2007


Author: luis
Date: 2007-04-24 11:18:26 -0700 (Tue, 24 Apr 2007)
New Revision: 6650

Added:
   cs/cigma/trunk/sandbox/dlopen/
   cs/cigma/trunk/sandbox/dlopen/Makefile
   cs/cigma/trunk/sandbox/dlopen/cos2.c
   cs/cigma/trunk/sandbox/dlopen/furls
   cs/cigma/trunk/sandbox/dlopen/hello.cpp
   cs/cigma/trunk/sandbox/dlopen/main1.cpp
   cs/cigma/trunk/sandbox/dlopen/main2.cpp
   cs/cigma/trunk/sandbox/dlopen/polygon.hpp
   cs/cigma/trunk/sandbox/dlopen/triangle.cpp
Log:
Using dynamically loaded functions


Added: cs/cigma/trunk/sandbox/dlopen/Makefile
===================================================================
--- cs/cigma/trunk/sandbox/dlopen/Makefile	2007-04-24 18:07:02 UTC (rev 6649)
+++ cs/cigma/trunk/sandbox/dlopen/Makefile	2007-04-24 18:18:26 UTC (rev 6650)
@@ -0,0 +1,30 @@
+TARGETS = cos2 main1 main2
+
+all: $(TARGETS)
+
+cos2: cos2.c
+	gcc -rdynamic $< -o $@ -ldl
+
+hello.o: hello.cpp
+	g++ -c $< -o $@
+
+hello.so: hello.o
+	g++ -shared $< -o $@
+
+main1: main1.cpp hello.so
+	g++ $^ -o $@ -ldl
+
+triangle.o: triangle.cpp
+	g++ -c $< -o $@
+
+triangle.so: triangle.o
+	g++ -shared $< -o $@
+
+main2: main2.cpp triangle.so
+	g++ $^ -o $@ -ldl
+
+clean:
+	rm -f *~ *.o *.so
+	rm -f $(TARGETS)
+
+.PHONY: clean
\ No newline at end of file

Added: cs/cigma/trunk/sandbox/dlopen/cos2.c
===================================================================
--- cs/cigma/trunk/sandbox/dlopen/cos2.c	2007-04-24 18:07:02 UTC (rev 6649)
+++ cs/cigma/trunk/sandbox/dlopen/cos2.c	2007-04-24 18:18:26 UTC (rev 6650)
@@ -0,0 +1,31 @@
+/* http://linux.about.com/library/cmd/blcmdl3_dlopen.htm
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+
+int main(int argc, char **argv)
+{
+    void *handle;
+    double (*cosine)(double);
+    char *error;
+
+    handle = dlopen("libm.so", RTLD_LAZY);
+    if (!handle)
+    {
+        fprintf(stderr, "%s\n", dlerror());
+        exit(1);
+    }
+
+    cosine = dlsym(handle, "cos");
+    if ((error = dlerror()) != NULL)
+    {
+        fprintf(stderr, "%s\n", error);
+        exit(1);
+    }
+
+    printf("%lf\n", (*cosine)(2.0));
+    dlclose(handle);
+    return 0;
+}

Added: cs/cigma/trunk/sandbox/dlopen/furls
===================================================================
--- cs/cigma/trunk/sandbox/dlopen/furls	2007-04-24 18:07:02 UTC (rev 6649)
+++ cs/cigma/trunk/sandbox/dlopen/furls	2007-04-24 18:18:26 UTC (rev 6650)
@@ -0,0 +1,6 @@
+# google: dlopen
+http://www.die.net/doc/linux/man/man3/dlopen.3.html
+http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/C++-dlopen-mini-HOWTO.html
+
+# google: gcc shared
+http://www.adp-gmbh.ch/cpp/gcc/create_lib.html

Added: cs/cigma/trunk/sandbox/dlopen/hello.cpp
===================================================================
--- cs/cigma/trunk/sandbox/dlopen/hello.cpp	2007-04-24 18:07:02 UTC (rev 6649)
+++ cs/cigma/trunk/sandbox/dlopen/hello.cpp	2007-04-24 18:18:26 UTC (rev 6650)
@@ -0,0 +1,7 @@
+#include <iostream>
+
+extern "C"
+void hello()
+{
+    std::cout << "hello!" << '\n';
+}

Added: cs/cigma/trunk/sandbox/dlopen/main1.cpp
===================================================================
--- cs/cigma/trunk/sandbox/dlopen/main1.cpp	2007-04-24 18:07:02 UTC (rev 6649)
+++ cs/cigma/trunk/sandbox/dlopen/main1.cpp	2007-04-24 18:18:26 UTC (rev 6650)
@@ -0,0 +1,44 @@
+#include <iostream>
+#include <dlfcn.h>
+
+int main()
+{
+    using std::cout;
+    using std::cerr;
+
+    cout << "C++ dlopen demo\n\n";
+
+    // open the library
+    cout << "Opening hello.so...\n";
+    void *handle = dlopen("./hello.so", RTLD_LAZY);
+    if (!handle)
+    {
+        cerr << "Cannot open library: " << dlerror() << '\n';
+        return 1;
+    }
+
+    // load the symbol
+    cout << "Loading symbol hello...\n";
+    typedef void (*hello_t)();
+
+    // reset errors
+    dlerror();
+    hello_t hello = (hello_t)dlsym(handle, "hello");
+    const char *dlsym_error = dlerror();
+    if (dlsym_error)
+    {
+        cerr << "Cannot load symbol 'hello': " << dlsym_error << '\n';
+        dlclose(handle);
+        return 1;
+    }
+
+    // use it to do the calculation
+    cout << "Calling hello...\n";
+    hello();
+
+    // close the library
+    cout << "Closing library...\n";
+    dlclose(handle);
+
+    return 0;
+}

Added: cs/cigma/trunk/sandbox/dlopen/main2.cpp
===================================================================
--- cs/cigma/trunk/sandbox/dlopen/main2.cpp	2007-04-24 18:07:02 UTC (rev 6649)
+++ cs/cigma/trunk/sandbox/dlopen/main2.cpp	2007-04-24 18:18:26 UTC (rev 6650)
@@ -0,0 +1,52 @@
+#include "polygon.hpp"
+#include <iostream>
+#include <dlfcn.h>
+
+int main()
+{
+    using std::cout;
+    using std::cerr;
+
+    // load the triangle library
+    void *triangle = dlopen("./triangle.so", RTLD_LAZY);
+    if (!triangle)
+    {
+        cerr << "Cannot load library: " << dlerror() << '\n';
+        return 1;
+    }
+
+    // reset errors
+    dlerror();
+
+    // load the symbols
+    create_t *create_triangle = (create_t *)dlsym(triangle, "create");
+    const char *dlsym_error = dlerror();
+    if (dlsym_error)
+    {
+        cerr << "Cannot load symbol create: " << dlsym_error << '\n';
+        return 1;
+    }
+
+    destroy_t *destroy_triangle = (destroy_t *)dlsym(triangle, "destroy");
+    dlsym_error = dlerror();
+    if (dlsym_error)
+    {
+        cerr << "Cannot load symbol destroy: " << dlsym_error << '\n';
+        return 1;
+    }
+
+    // create an instance of the class
+    polygon *poly = create_triangle();
+
+    // use the class
+    poly->set_side_length(7);
+    cout << "The area is: " << poly->area() << '\n';
+
+    // destroy the class
+    destroy_triangle(poly);
+
+    // unload the triangle library
+    dlclose(triangle);
+
+    return 0;
+}

Added: cs/cigma/trunk/sandbox/dlopen/polygon.hpp
===================================================================
--- cs/cigma/trunk/sandbox/dlopen/polygon.hpp	2007-04-24 18:07:02 UTC (rev 6649)
+++ cs/cigma/trunk/sandbox/dlopen/polygon.hpp	2007-04-24 18:18:26 UTC (rev 6650)
@@ -0,0 +1,27 @@
+#ifndef POLYGON_HPP
+#define POLYGON_HPP
+
+class polygon
+{
+protected:
+    double _side_length;
+
+public:
+
+    polygon() : _side_length(0) {}
+
+    virtual ~polygon() {}
+
+    void set_side_length(double side_length)
+    {
+        _side_length = side_length;
+    }
+
+    virtual double area() const = 0;
+};
+
+// the types of the class factories
+typedef polygon *create_t();
+typedef void destroy_t(polygon *);
+
+#endif

Added: cs/cigma/trunk/sandbox/dlopen/triangle.cpp
===================================================================
--- cs/cigma/trunk/sandbox/dlopen/triangle.cpp	2007-04-24 18:07:02 UTC (rev 6649)
+++ cs/cigma/trunk/sandbox/dlopen/triangle.cpp	2007-04-24 18:18:26 UTC (rev 6650)
@@ -0,0 +1,23 @@
+#include "polygon.hpp"
+#include <cmath>
+
+class triangle : public polygon
+{
+public:
+    virtual double area() const
+    {
+        return _side_length * _side_length * sqrt(3) / 2;
+    }
+};
+
+// the class factories
+
+extern "C" polygon *create()
+{
+    return new triangle;
+}
+
+extern "C" void destroy(polygon *p)
+{
+    delete p;
+}



More information about the cig-commits mailing list