title: C Dynamic loading file too short?
body: <p>I'm trying out dynamic loading with C and I've run into a problem at the very set out.  I have a small program that loads an object at run time.  The object contains a single function that writes some messages to stdin.  This has been compiled on OS X 10.10 with clang.  Here is the code:</p>
  
  <pre><code>/* loader.c */
  
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;dlfcn.h&gt;
  #include "module.h"
  
  int main(int argc, char **argv) {
  
      char file[] = "/users/user/dev/module.o";
      void *handle;
      int (*function)();
  
      handle = dlopen(file, RTLD_NOW);
  
      if (!handle) {
  
          printf("Cannot load program. Error: %s\n", dlerror());
  
          return 1;
  
      }
  
      program = dlsym(handle, "function");
  
      printf("Program loaded");
  
      function();
  
      printf("Exiting");
  
      return 0;
  
  }
  </code></pre>
  
  <p>Here is module.h:</p>
  
  <pre><code>/* module.h */
  
  int
  function();
  </code></pre>
  
  <p>Here is module.c:</p>
  
  <pre><code>/* module.c */
  
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  int function() {
  
      printf("Hello from module");
      sleep(1);
      printf("Hello from module again");
  
      return 0;
  }
  </code></pre>
  
  <p>Here is the Makefile:</p>
  
  <pre><code>loader : loader.c module.o
      cc  -Wall loader.c -ldl -o loader
  
  module.o : module.c
      cc -Wall -fpic -c module.c
  </code></pre>
  
  <p>This compiles without warnings, but it does not execute the way I expect.  The program returns the following error:</p>
  
  <p><code>Error: dlopen(/users/user/dev/module.o, 2): no suitable image found.  Did find: /users/user/dev/module.o: file too short</code>.</p>
  
  <p>I've had a look and there is not very much out there on this error message.  This program was based on the <code>dlopen</code> example from TLDP.  Does this mean that the files need to be of a certain size in order to be dynamically loaded, or is there something wrong with the way these files have been compiled?</p>
  
  <p>I feel like I'm missing something simple.</p>
  
  <p>How do I get this program to execute as expected?</p>