1 #include <dlfcn.h>
2 #include <iostream>
3 
4 #define SO_CLOSE_RECURSIVE "libdlclose_recursive.so"
5 
6 extern "C" {
Add(int a, int b)7 int Add(int a, int b)
8 {
9     return a + b;
10 }
11 }
12 
13 class BarIns {
14 public:
BarIns()15 	BarIns()
16 	{
17             handle = dlopen(SO_CLOSE_RECURSIVE, RTLD_LOCAL);
18             if (!handle)
19             {
20                 std::cerr << "dlopen(name=" << SO_CLOSE_RECURSIVE \
21                     << ",mode=" << RTLD_LOCAL \
22                     << ",failed: " << dlerror() << std::endl;
23             }
24             std::cerr << "open: " << SO_CLOSE_RECURSIVE << " successfully" << std::endl;
25 	}
26 
~BarIns()27         ~BarIns()
28         {
29 	    if (handle != nullptr)
30 	    {
31                 dlclose(handle);
32             }
33             std::cerr << "close: " << SO_CLOSE_RECURSIVE << " successfully" << std::endl;
34 	}
35 
GetInstance()36 	static BarIns& GetInstance()
37 	{
38 		return bi;
39 	}
40 
41 private:
42 	static BarIns bi;
43 	void *handle {nullptr};
44 };
45 
46 BarIns BarIns::bi;
47