gpt4 book ai didi

c - 如何在 OS X 中包含 FFI?

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:05 25 4
gpt4 key购买 nike

我在构建时遇到问题 this project启用 FFI 扩展。为了隔离问题,我正在尝试编译 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;
}

最佳答案

好吧,看来你可以这样做来解决它:

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

这将构建 libffi来自源代码,即使你有 libffi 它也会工作从 brew 安装,因为这个新版本将安装在 /usr/local 下而不是 Cellar .

那么你不需要改变#include ,保持它像#include <ffi.h> , 打开一个终端到你的独立 c 源所在的文件夹并发出 gcc -o ffi -lffi ffi.c (假设它是您拥有的源文件)。

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

关于c - 如何在 OS X 中包含 FFI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48308803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com