Common lisp、CFFI 和实例化 C 结构

发布于 2024-10-10 10:49:09 字数 1043 浏览 5 评论 0原文

我已经在谷歌上搜索了大约,哦,三个小时,寻找这个“问题”的解决方案。我试图弄清楚如何使用 CFFI 在 lisp 中实例化 C 结构。我在 c 中有一个结构:

struct cpVect{cpFloat x,y;}

简单吧?我已经自动生成了到此结构的 CFFI 绑定(我认为是 swig):

(cffi:defcstruct #.(chipmunk-lispify "cpVect" 'classname)
    (#.(chipmunk-lispify "x" 'slotname) :double)
    (#.(chipmunk-lispify "y" 'slotname) :double))

这会生成一个带有插槽 :X 和 :Y 的结构“VECT”,外来插槽名称确认了这一点(请注意,我既没有生成绑定,也没有编程C 库(花栗鼠物理学),但实际的函数是从 lisp 调用的就好)。

我进行了广泛的搜索,也许我已经看过它 100 次并掩盖了它,但我无法弄清楚如何在 lisp 中创建 cpVect 实例以在其他函数中使用。

请注意该函数:

cpShape *cpPolyShapeNew(cpBody *body, int numVerts, cpVect *verts, cpVect offset)

不仅采用 cpVect,还采用指向一组 cpVect 的指针,这引出了我的第二个问题:如何创建指向一组结构的指针?

我去过 http://common-lisp.net/project/ cffi/manual/html_node/defcstruct.html 并尝试了代码,但得到“错误:未绑定变量:PTR”(我在 Clozure CL 中),更不用说看起来只返回一个指针,而不是一个实例。

我是 lisp 的新手,到目前为止已经很强大了,但这是我遇到的第一个真正的问题,我无法解决。谢谢!

I've been on google for about, oh, 3 hours looking for a solution to this "problem." I'm trying to figure out how to instantiate a C structure in lisp using CFFI. I have a struct in c:

struct cpVect{cpFloat x,y;}

Simple right? I have auto-generated CFFI bindings (swig, I think) to this struct:

(cffi:defcstruct #.(chipmunk-lispify "cpVect" 'classname)
    (#.(chipmunk-lispify "x" 'slotname) :double)
    (#.(chipmunk-lispify "y" 'slotname) :double))

This generates a struct "VECT" with slots :X and :Y, which foreign-slot-names confirms (please note that I neither generated the bindings or programmed the C library (chipmunk physics), but the actual functions are being called from lisp just fine).

I've searched far and wide, and maybe I've seen it 100 times and glossed over it, but I cannot figure out how to create a instance of cpVect in lisp to use in other functions.

Note the function:

cpShape *cpPolyShapeNew(cpBody *body, int numVerts, cpVect *verts, cpVect offset)

Takes not only a cpVect, but also a pointer to a set of cpVects, which brings me to my second question: how do I create a pointer to a set of structs?

I've been to http://common-lisp.net/project/cffi/manual/html_node/defcstruct.html and tried the code, but get "Error: Unbound variable: PTR" (I'm in Clozure CL), not to mention that looks to only return a pointer, not an instance.

I'm new to lisp, been going pretty strong so far, but this is the first real problem I've hit that I can't figure out. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮光沉寂 2024-10-17 10:49:09

大多数 Common Lisp 实现不允许在堆栈上传递结构。有一个 fsbv 库,它使用 libffi 来添加该功能。如果您知道结构布局,您可以将其手动分解为一系列基本参数,但这显然很脆弱。

Most Common Lisp implementations do not allow passing structures on stack. There is a fsbv library which uses libffi to add that capability. If you know the structure layout you can decompose it manually as a series of basic arguments, but that is obviously brittle.

岁月蹉跎了容颜 2024-10-17 10:49:09

不确定这会有多大帮助,但您可以查看花栗鼠的 ruby​​ FFI 绑定:
https://github.com/shawn42/chipmunk -ffi/blob/master/lib/chipmunk-ffi/vec2.rb

Chipmunk 有一个数据部分,其中包含所有内联方法调用,以便 FFI 可以使用它们。其中之一称为 cpv。 cpv 是创建 cpVect 结构的方法。请随意在 github 上与我 (shawn42) 展开有关此问题的对话。

Not sure this will help much, but you could look at the ruby FFI bindings for chipmunk:
https://github.com/shawn42/chipmunk-ffi/blob/master/lib/chipmunk-ffi/vec2.rb

Chipmunk has a data section that has all the inline method calls so FFI can use them. One of those is called cpv. cpv is the method that creates the cpVect struct. Feel free to start up a conversation about this on github w/ me (shawn42).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文