在OSX上与LD_PRELOAD完全等价的是什么?

hor*_*guy 14 macos linker gcc ld-preload

我正在使用LD_PRELOAD钩子库函数,在Linux中它工作得很好.但我无法弄清楚如何在OSX中做同等效果.

我在Linux上的设置如下:

代码是:

#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <unistd.h>
#include <ruby.h>

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;
}
Run Code Online (Sandbox Code Playgroud)

然后我编译:

gcc -Wall -O2 -fpic -shared -ldl -g -I/path/to/includes/ -o raise_shim.so raise_shim.c
Run Code Online (Sandbox Code Playgroud)

然后我执行:

LD_PRELOAD=./raise_shim.so ruby
Run Code Online (Sandbox Code Playgroud)

所有上述内容在Linux上运行良好,在OSX上运行这一步的每一步都等同于什么?我已经搜索了这个并且无法使用所提供的信息,因为缺少某些步骤的信息.

提前致谢!

Dan*_*ego 16

看看DYLD_INSERT_LIBRARIES.这是你正在寻找的变量.

另见这个答案.