title: Loading so files with dlsym, cannot load library
body: <p>I am implementing an API which loads plug-ins from .so files. To load them I am using the dlsym() function. To check if it works I have implemented a test plug-in (below) and to make things easier I am loading only the "initialize" function. To compile my code I am firstly creating the .so file with the following command:</p>
  
  <pre><code>      g++ -Wall -shared -rdynamic -fPIC plugin_test.cpp -o plugin_test.so
  </code></pre>
  
  <p>After that I run the plugin_manager.cpp as follows:</p>
  
  <pre><code>      g++ plugin_manager.cpp -o -plugin_manager.o -ldl
  </code></pre>
  
  <p>But after running the a.out and typing the correct filename, I get an error that I cannot load the .so file. Does anyone have a clue about how to solve this issue?</p>
  
  <p>plugin_manager.cpp</p>
  
  <pre><code>      #include &lt;dlfcn.h&gt;
        #include &lt;stdio.h&gt;
        #include &lt;stdlib.h&gt;
        #include &lt;string&gt;
        #include &lt;iostream&gt;
        #include "plugin_interface.h"
        using namespace std;
        //typedef void (*test_function)(void);
  
        int main(void) {
         const char *error;
         void *module;
         //test_function initialize_test;
         //void (*initialize_test) (void);
         //test_function analyze_test;
         //test_function finalize_test;
         //load the DL library
         string filename;
         cout &lt;&lt; "File :: " &lt;&lt; endl;
         cin &gt;&gt; filename;
         module = dlopen(filename.c_str(), RTLD_LAZY);
         if (!module) {
             fprintf(stderr, "ERROR! Couldn't load library: %s\n",
             dlerror());
             exit(1);
         }
  
          //get and check symbol
          dlerror();
          initialize_f* initialize_function = (initialize_f*) dlsym(module, "initialize");
          if ((error = dlerror())) {
          fprintf(stderr, "ERROR %s\n", error);
          exit(1);
          }
  
          //create instance of the class
          PluginInterface* plugin = initialize_function();
  
          //use the class
          plugin-&gt;initialize();
          //call "initialize" function from the test plug-in
          //(*initialize_test)();
  
          dlclose(module); //close the DL library
          return 0;
          }
  </code></pre>
  
  <p>plugin_interface.h</p>
  
  <pre><code>    #ifndef PLUGIN_INTERFACE_H_INCLUDED
      #define PLUGIN_INTERFACE_H_INCLUDED
  
      class PluginInterface{
  
      public:
          virtual void initialize() = 0;
          virtual void analyze() = 0;
          virtual void finalize() = 0;
      };
  
      //Definition of the pointer
      typedef PluginInterface* initialize_f();
      #endif // PLUGIN_INTERFACE_H_INCLUDED
  </code></pre>
  
  <p>test_plugin.cpp</p>
  
  <pre><code>         #include &lt;iostream&gt;
           #include "plugin_interface.h"
           using namespace std;
  
           class bbb :public PluginInterface {
             public:
  
           virtual void initialize(){
           cout &lt;&lt; "Initialization";
           }
  
           void analyze(){
           cout &lt;&lt; "Analysis";
           } 
           void finalize(){
           cout &lt;&lt; "Finalization";
           }
  
           };
  
           //The functions we load must be qualified as extern "C" to avoid the symbol name being mangled
           extern "C" PluginInterface* initialize(){
           return new bbb();
           }
           int main()
           {
  
  
           //bbb* a = maker();
           //a-&gt;initialize();
           //Creating an instance and calling the "initialize" function with in
           PluginInterface* p = initialize();
           p-&gt;initialize(); 
  
  
           return 0;
           }
  </code></pre>