title: What is the exact equivalent to LD_PRELOAD on OSX?
body: <p>I am using <code>LD_PRELOAD</code> to hook a library function, and in Linux it works perfectly. But I cannot figure out how to do the equivalent in OSX.</p>
  
  <p>The setup I have on Linux is as follows: </p>
  
  <p>The code is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;dlfcn.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;ruby.h&gt;
  
  void
  rb_raise(unsigned long exc, const char *fmt, ...)
  {
    static void (*libruby_rb_raise)
      (unsigned long exc, const char *fmt, ...) = NULL;
  
    void * handle;
    char * error;
  
    if (!libruby_rb_raise) {
      handle = dlopen("/path/to/libruby.so",
                      RTLD_LAZY);
      if (!handle) {
        fputs(dlerror(), stderr);
        exit(1);
      }
      libruby_rb_raise = dlsym(handle, "rb_raise");
      if ((error = dlerror()) != NULL) {
        fprintf(stderr, "%s\n", error);
        exit(1);
      }
    }
  
    // ...code... 
  
    return Qnil;
  }
  </code></pre>
  
  <p>Which I then compile with: </p>
  
  <pre><code>gcc -Wall -O2 -fpic -shared -ldl -g -I/path/to/includes/ -o raise_shim.so raise_shim.c
  </code></pre>
  
  <p>I then execute with:</p>
  
  <pre><code>LD_PRELOAD=./raise_shim.so ruby
  </code></pre>
  
  <p>All of the above works well on Linux, what is the equivalent for each step to get this working on OSX? I have googled this and have not been able to get it to work with the information provided as the info for some of the steps are missing.</p>
  
  <p>Thanks in advance!</p>