使用带有 Ruby ext 的 mkmf 使用 -L 和 -I 和 -l 链接静态库

Using mkmf with Ruby ext Linking a Static Library with -L and -I and -l

给定一个简单的 gcc 命令如下:

gcc quickrb.c -o main -L /usr/local/lib/quickjs -I /usr/local/lib/quickjs -l quickjs

我正在尝试使用 mkmf 将其包装在 ruby gem 扩展 extconf.rb 中。目前我有:

require 'mkmf'

dir_config('quickjs', '/usr/local/lib/quickjs', '/usr/local/include/quickjs')

abort('missing "quickjs.h"') unless find_header('quickjs/quickjs.h')
abort('missing JS_NewRuntime') unless find_library('quickjs', 'JS_NewRuntime', 'quickjs/quickjs.h')
abort('missing JS_NewContext') unless find_library('quickjs', 'JS_NewContext', 'quickjs/quickjs.h')

create_makefile('quickrb/quickrb')

这失败了:

checking for quickjs/quickjs.h... yes
checking for JS_NewRuntime() in -lquickjs... no
missing JS_NewRuntime

我不确定如何处理。没有 find_library 调用 Makefile 生成,但是在编译时失败:

dyld: Symbol not found: _JS_NewRuntime

:

这是我的示例 quickrb.c 文件:

#include <quickjs/quickjs.h>

#include <ruby.h>

#include <stdio.h>
#include <strings.h>

void Init_quickrb()
{
  const char *filename = "runtime";
  const char *script = "3 + 4";
  const size_t length = strlen(script);

  JSRuntime *runtime = JS_NewRuntime();
  JSContext *context = JS_NewContext(runtime);

  JSValue value = JS_Eval(context, script, length, filename, JS_EVAL_TYPE_GLOBAL);

  const char *result = JS_ToCString(context, value);
  printf("%s = %s\n", script, result);
  JS_FreeCString(context, result);

  JS_FreeContext(context);
  JS_FreeRuntime(runtime);
}

我想你想要的是:

find_library('quickjs', 'JS_NewRuntime', '/usr/local/lib/quickjs')

因为 the documentation 说:

find_library(lib, func, *paths, &b) public

Returns whether or not the entry point func can be found within the library lib in one of the paths specified, where paths is an array of strings. If func is nil , then the main() function is used as the entry point.

If lib is found, then the path it was found on is added to the list of library paths searched and linked against.