如何在OS X中包含FFI? [英] How to include FFI in OS X?

查看:225
本文介绍了如何在OS X中包含FFI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在构建启用了FFI扩展的此项目时遇到问题.为了找出问题,我尝试编译此示例(完整包含在下面).

I'm having trouble building this project with the FFI extension enabled. To isolate the problem, I'm trying to compile this example (included in full below).

我正在使用安装了命令行工具的OS X 10.13.2,Xcode 9.2(确认存在/usr/include/ffi/ffi.h ).我修改了示例,使包含行显示为include <ffi/ffi.h>.

I'm using OS X 10.13.2, Xcode 9.2 with Command Line Tools installed (confirmed that /usr/include/ffi/ffi.h exists). I modified the example so the include line reads include <ffi/ffi.h>.

调用不带任何选项的编译器,我得到以下信息:

Invoking the compiler with no options, I get the following:

$ gcc closure.test.c
closure.test.c:23:13: warning: implicit declaration of function 'ffi_closure_alloc' is invalid in C99 [-Wimplicit-function-declaration]
  closure = ffi_closure_alloc(sizeof(ffi_closure), &bound_puts);
            ^
closure.test.c:23:11: warning: incompatible integer to pointer conversion assigning to 'ffi_closure *' (aka 'struct ffi_closure *') from
      'int' [-Wint-conversion]
  closure = ffi_closure_alloc(sizeof(ffi_closure), &bound_puts);
          ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
closure.test.c:35:15: warning: implicit declaration of function 'ffi_prep_closure_loc' is invalid in C99 [-Wimplicit-function-declaration]
          if (ffi_prep_closure_loc(closure, &cif, puts_binding,
              ^
closure.test.c:45:3: warning: implicit declaration of function 'ffi_closure_free' is invalid in C99 [-Wimplicit-function-declaration]
  ffi_closure_free(closure);
  ^
4 warnings generated.
Undefined symbols for architecture x86_64:
  "_ffi_closure_alloc", referenced from:
      _main in closure-7b0e9b.o
  "_ffi_closure_free", referenced from:
      _main in closure-7b0e9b.o
  "_ffi_prep_cif", referenced from:
      _main in closure-7b0e9b.o
  "_ffi_prep_closure_loc", referenced from:
      _main in closure-7b0e9b.o
  "_ffi_type_pointer", referenced from:
      _main in closure-7b0e9b.o
  "_ffi_type_sint32", referenced from:
      _main in closure-7b0e9b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我需要哪些选项/修改来纠正此问题?

What options/modifications do I need to correct this?

#include <stdio.h>
#include <ffi.h>

/* Acts like puts with the file given at time of enclosure. */
void puts_binding(ffi_cif *cif, void *ret, void* args[],
                  void *stream)
{
  *(ffi_arg *)ret = fputs(*(char **)args[0], (FILE *)stream);
}

typedef int (*puts_t)(char *);

int main()
{
  ffi_cif cif;
  ffi_type *args[1];
  ffi_closure *closure;

  void *bound_puts;
  int rc;

  /* Allocate closure and bound_puts */
  closure = ffi_closure_alloc(sizeof(ffi_closure), &bound_puts);

  if (closure)
    {
      /* Initialize the argument info vectors */
      args[0] = &ffi_type_pointer;

      /* Initialize the cif */
      if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
                       &ffi_type_sint, args) == FFI_OK)
        {
          /* Initialize the closure, setting stream to stdout */
          if (ffi_prep_closure_loc(closure, &cif, puts_binding,
                                   stdout, bound_puts) == FFI_OK)
            {
              rc = ((puts_t)bound_puts)("Hello World!");
              /* rc now holds the result of the call to fputs */
            }
        }
    }

  /* Deallocate both closure, and bound_puts */
  ffi_closure_free(closure);

  return 0;
}

推荐答案

好吧,看来您可以这样做:

Well, seems like you can fix it doing this:

$ cd
$ git clone https://github.com/libffi/libffi
$ cd libffi/
$ ./autogen.sh
$ ./configure
$ make
$ make install

这将从源代码构建libffi,即使从brew安装了libffi,它也将起作用,因为此新版本将安装在/usr/local下,而不是在Cellar中.

This will build libffi from source and it will work even if you have libffi installed from brew, because this new version will be installed under /usr/local and not in Cellar.

然后,您无需更改#include,将其保留为#include <ffi.h>,在您隔离的c源所在的文件夹中打开终端,然后发出gcc -o ffi -lffi ffi.c(假定它是您拥有的源文件). /p>

Then you don't need to change the #include, keep it like #include <ffi.h>, open a terminal to the folder your isolated c source is and issue gcc -o ffi -lffi ffi.c (assuming it's the source file you have).

teixeira: ~/etudes_c $ ./ffi
Hello World!

这篇关于如何在OS X中包含FFI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆