title: Dynamic Loading Without extern "C"
body: <p>I'd like to use libdl to dynamically load C++ in general. The problem is identifying symbols at runtime that have been name mangled.</p>
  
  <p>As described here, one solution is to remove name mangling by using extern "C".</p>
  
  <p><a href="http://www.tldp.org/HOWTO/C++-dlopen/theproblem.html" rel="noreferrer">http://www.tldp.org/HOWTO/C++-dlopen/theproblem.html</a></p>
  
  <p>This solution has the drawback of limiting dynamically loaded resources to C style interfaces. Dynamically loaded functions cannot, for instance, be overloaded functions.</p>
  
  <p>What is a good way to overcome this limitation?</p>
  
  <p>One possible solution would be tools to name mangle the library source code with an accompanying function to get the mangled names when the library needs to be linked. Does llvm provide tools for this?</p>
  
  <p>Maybe a clumsy solution would be a function that takes a function signature, creates dummy code with a function that has the signature, pipes into the compiler that was used with a flag for generating assembly, parses the output to retrieve the mangled name, and returns the mangled name as a string. The string could then be passed to dlsym().</p>
  
  <p>To keep the problem concrete, here are two example programs that illustrate something the extern "C" solution can't dynamically load without modifying library code. The first dynamically links a library in traditional C++ fashion. The second uses dlopen. Linking an overloaded function in the first program is simple. There's no simple way to link the overloaded function in the second program.</p>
  
  <p><strong>Program 1: Loadtime Dynamic Linking</strong></p>
  
  <p>main.cpp</p>
  
  <pre><code>// forward declarations of functions that will be linked
  void say(int);
  void say(float);
  
  int main() {
      int myint = 3;
      say(myint);
      float myfloat = 5.0f;
      say(myfloat);
  }
  </code></pre>
  
  <p>say.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  //extern "C" function signatures would collide
  
  //extern "C" void say(int a) {
  void say(int a) {
      std::cout &lt;&lt; "The int value is " &lt;&lt; a &lt;&lt; ".\n";
  }
  
  //extern "C" void say(float a) {
  void say(float r) {
      std::cout &lt;&lt; "The float value is " &lt;&lt; r &lt;&lt; ".\n";
  }
  </code></pre>
  
  <p>output</p>
  
  <pre><code>$ ./main
  The int value is 3.
  The float value is 5.
  </code></pre>
  
  <p><strong>Program 2: Runtime Dynamic Linking</strong></p>
  
  <p>main_with_dl.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;dlfcn.h&gt;
  
  int main() {
      // open library
      void* handle = dlopen("./say_externC.so", RTLD_LAZY);
      if (!handle) {
          std::cerr &lt;&lt; "dlopen error: " &lt;&lt; dlerror() &lt;&lt; '\n';
          return 1;
      }
  
      // load symbol
      typedef void (*say_t)(int);
  
      // clear errors, find symbol, check errors
      dlerror();
      say_t say = (say_t) dlsym(handle, "say");
      const char *dlsym_error = dlerror();
      if (dlsym_error) {
          std::cerr &lt;&lt; "dlsym error: " &lt;&lt; dlsym_error &lt;&lt; '\n';
          dlclose(handle);
          return 1;
      }
  
      // use function
      int myint = 3;
      say(myint);
      // can't load in void say(float)
      // float myfloat = 5.0f;
      // say(myfloat);
  
      // close library
      dlclose(handle);
  }
  </code></pre>
  
  <p>output</p>
  
  <pre><code>$ ./main_with_dl
  The int value is 3.
  </code></pre>
  
  <p><strong>Compiling</strong></p>
  
  <p>Makefile</p>
  
  <pre><code>CXX = g++
  
  all: main main_with_dl say_externC.so
  
  main: main.cpp say.so
      $(CXX) -o $@ $^
  
  main_with_dl: main_with_dl.cpp
      $(CXX) -o $@ $&lt;
  
  %.so : %.cpp
      $(CXX) -shared -o $@ $&lt;
  
  .PHONY: clean
  clean:
      rm main main_with_dl say.so say_externC.so
  </code></pre>