summaryrefslogtreecommitdiffstats
path: root/doc/sources/c++/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'doc/sources/c++/main.cpp')
-rw-r--r--doc/sources/c++/main.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/doc/sources/c++/main.cpp b/doc/sources/c++/main.cpp
new file mode 100644
index 0000000..b8fb8cd
--- /dev/null
+++ b/doc/sources/c++/main.cpp
@@ -0,0 +1,44 @@
+#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;
+ }
+
+ // load the symbols
+ create_t* create_triangle = (create_t*) dlsym(triangle, "create");
+ destroy_t* destroy_triangle = (destroy_t*) dlsym(triangle, "destroy");
+
+ if (!create_triangle || !destroy_triangle)
+ {
+ cerr << "Cannot load symbols: " << dlerror() << '\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;
+}