【发布时间】:2018-01-17 19:42:50
【问题描述】:
在启用 FFI 扩展的情况下构建 this project 时遇到问题。为了隔离问题,我正在尝试编译this example(完整包含在下面)。
我正在使用安装了命令行工具的 OS X 10.13.2、Xcode 9.2(确认 /usr/include/ffi/ffi.h 存在)。我修改了示例,因此包含行显示为include <ffi/ffi.h>。
在没有选项的情况下调用编译器,我得到以下信息:
$ 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)
我需要哪些选项/修改来纠正这个问题?
closure.test.c的来源:
#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;
}
【问题讨论】:
-
坏消息:Mac OS 确实没有您缺少的功能。查看this,尤其是闭包分配部分。
-
@user58697 很奇怪,真的吗?我看到在 MacOS 上缺少
ffi_prep_cif_var的一些佐证。但是闭包似乎是存在的...关于 MacOS 上闭包中当前错误的讨论...例如this one 最近提到它,ffi_closure_alloc被称为here in 2009。他们在谈论不同的事情吗? -
bugs.python.org/msg115649 是更多的证据。