title: How to compile a program using static library libdl.a
body: <p>I am trying to compile the example code which is using APIs from libdl library:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;dlfcn.h&gt;
  
  int
  main(int argc, char **argv)
  {
      void *handle;
      double (*cosine)(double);
      char *error;
  
     handle = dlopen("libm.so", RTLD_LAZY);
      if (!handle) {
          fprintf(stderr, "%s\n", dlerror());
          exit(EXIT_FAILURE);
      }
  
     dlerror();    /* Clear any existing error */
  
     /* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
         would seem more natural, but the C99 standard leaves
         casting from "void *" to a function pointer undefined.
         The assignment used below is the POSIX.1-2003 (Technical
         Corrigendum 1) workaround; see the Rationale for the
         POSIX specification of dlsym(). */
  
     *(void **) (&amp;cosine) = dlsym(handle, "cos");
  
     if ((error = dlerror()) != NULL)  {
          fprintf(stderr, "%s\n", error);
          exit(EXIT_FAILURE);
      }
  
     printf("%f\n", (*cosine)(2.0));
      dlclose(handle);
      exit(EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>I used the following command to compile:
  --> gcc -static -o foo foo.c -ldl</p>
  
  <p>I got the following error:</p>
  
  <pre><code>foo.c:(.text+0x1a): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
  </code></pre>
  
  <p>After google, since I am trying to compile it statically, I can find the libdl.a in the lib directory. I am getting the same issue with gethostbyname API also..
  What are the other libraries needs to add to compile dl_open statically.</p>