我正在尝试使用FFI实现来自Darknet library的几个函数
require 'ffi'
class Image < FFI::Struct
layout :w, :int
layout :h, :int
layout :c, :int
layout :data, :pointer
end
class Darknet
extend FFI::Library
ffi_lib "./libdarknet.so"
attach_function :load_image_color, [:string, :int, :int], Image
end
image = Darknet.load_image_color("./test.jpg", 0, 0)
文件名字符串似乎没有通过:
Cannot load image "(null)"
STB Reason: can't fopen
这里是the function和the declaration。
我是FFI的新手,但我已经成功地实现了其他功能,没有任何问题。我肯定遗漏了一些明显的东西,如果有人能给我指点(没有双关语)…
发布于 2018-06-19 07:33:38
Ruby字符串与c字符串(或char []
)不同,C字符串是以null结尾(最后一个字符是ASCII0- '\0'
)的连续字符数组,通常被认为是c中的“char *
”。
因此,您需要通过宏StringValueCStr(str)
(其中str
是字符串变量"./test.jpg"
)将拼音字符串转换为指针。还有一种选择是StringValuePtr(str)
,它在性能方面更好,但是它并不是完全安全的,因为它没有考虑到c字符串是以null结尾的,因此字符串中不能有null结束符(这是Ruby字符串允许的)。另一方面,StringValueCStr()
确实考虑到了这一点,因此可以确保您有一个有效的c字符串。
https://stackoverflow.com/questions/50917280
复制相似问题