我在构建时遇到问题这个项目 https://github.com/metaeducation/ren-c启用 FFI 扩展。为了隔离问题,我正在尝试编译这个例子 http://www.chiark.greenend.org.uk/doc/libffi-dev/html/Closure-Example.html(全文包含在下面)。
我正在使用 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;
}