title: How to export specific symbol from executables in GNU/Linux
body: <p>While loading dynamic libraries by <code>::dlopen()</code>, exporting symbols from executables can be done by <code>-rdynamic</code> option, but it exports all the symbols of the executable, which results in bigger binary size.</p>
  
  <p>Is there a way to export just specific function(s)?</p>
  
  <p>For example, I have testlib.cpp and main.cpp as below:</p>
  
  <p><strong>testlib.cpp</strong></p>
  
  <pre><code>extern void func_export(int i);
  
  extern "C" void func_test(void)
  {
    func_export(4);
  }
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;dlfcn.h&gt;
  
  void func_export(int i)
  {
    ::fprintf(stderr, "%s: %d\n", __func__, i);
  }
  
  void func_not_export(int i)
  {
    ::fprintf(stderr, "%s: %d\n", __func__, i);
  }
  
  typedef void (*void_func)(void);
  
  int main(void)
  {
    void* handle = NULL;
    void_func func = NULL;
    handle = ::dlopen("./libtestlib.so", RTLD_NOW | RTLD_GLOBAL);
    if (handle == NULL) {
      fprintf(stderr, "Unable to open lib: %s\n", ::dlerror());
      return 1;
    }
    func = reinterpret_cast&lt;void_func&gt;(::dlsym(handle, "func_test"));
  
    if (func == NULL) {
      fprintf(stderr, "Unable to get symbol\n");
      return 1;
    }
    func();
    return 0;
  }
  </code></pre>
  
  <p><strong>Compile:</strong></p>
  
  <pre><code>g++ -fPIC -shared -o libtestlib.so testlib.cpp
  g++ -c -o main.o main.cpp
  </code></pre>
  
  <p>I want func_export to be used by the dynamic library, but hide the func_not_export.</p>
  
  <p>If link with -rdynamic, 
  <code>g++ -o main -ldl -rdynamic main.o</code>
  , both functions are exported.</p>
  
  <p>If not link with -rdynamic,
  <code>g++ -o main_no_rdynamic -ldl main.o</code>
  , I got runtime error <code>Unable to open lib: ./libtestlib.so: undefined symbol: _Z11func_exporti</code></p>
  
  <p>Is it possible to achieve the requirement that only export the specific function?</p>