使用FFI为Ruby包装C函数

时间:2016-01-12 23:39:59

标签: c++ c ruby ffi

我承认自己很难使用C和C ++,但是我试图在FFI中包装一个C ++函数(extern to' C')但是我并不完全理解语法。

外部C代码的形式是:

Client *create_client(Address *address, const char * const foo){
//make stuff  
}

我的问题,具体来说,是如何使用FFI将其包装在Ruby中?我认为构造函数有两个指针,所以形式为:

attach_function :create_client, [:pointer, :pointer], :pointer

但是,另一方面,const char * const foo让我困惑 - 如果它被视为一个字符串,就像这样:

attach_function :create_client, [:pointer, :string], :pointer

感谢。

1 个答案:

答案 0 :(得分:1)

attach_function :create_client, [:pointer, :string], :pointer

const char *表示它是一个c字符串const部分,只对您的调用函数有影响。有关详细信息,请参阅this。你包装的功能看起来对我来说是正确的,但我假设你需要的功能比这个功能更复杂。

FFI wiki有一些很好的信息here