如何在 C 绑定中拥有数组类型?
How to have an array type in C binding?
我正在尝试移植此代码:
struct SoundIoChannelLayout {
const char *name;
int channel_count;
enum SoundIoChannelId channels[SOUNDIO_MAX_CHANNELS];
};
但我不知道如何定义 channels
的类型,而且我知道我不能使用指针,因为最终的结构大小不会相同。
我能够使用 crystal_lib:
自动生成它
$ cd crystal_lib
$ cat examples/soundio.cr
@[Include("soundio/soundio.h", prefix: %w(SoundIo))]
@[Link("soundio")]
lib LibSoundio
end
$ crystal src/main.cr -- examples/soundio.cr > soundio.cr
所以看起来像这样:
@[Link("soundio")]
lib LibSoundio
MAX_CHANNELS = 24
struct ChannelLayout
name : LibC::Char*
channel_count : LibC::Int
channels : ChannelId[MAX_CHANNELS]
end
enum ChannelId
Invalid = 0
FrontLeft = 1
FrontRight = 2
FrontCenter = 3
# ...
end
# ...
end
注意:您可能需要手动更新结果文件,因为crystal_lib仍处于试验阶段。
我正在尝试移植此代码:
struct SoundIoChannelLayout {
const char *name;
int channel_count;
enum SoundIoChannelId channels[SOUNDIO_MAX_CHANNELS];
};
但我不知道如何定义 channels
的类型,而且我知道我不能使用指针,因为最终的结构大小不会相同。
我能够使用 crystal_lib:
自动生成它$ cd crystal_lib
$ cat examples/soundio.cr
@[Include("soundio/soundio.h", prefix: %w(SoundIo))]
@[Link("soundio")]
lib LibSoundio
end
$ crystal src/main.cr -- examples/soundio.cr > soundio.cr
所以看起来像这样:
@[Link("soundio")]
lib LibSoundio
MAX_CHANNELS = 24
struct ChannelLayout
name : LibC::Char*
channel_count : LibC::Int
channels : ChannelId[MAX_CHANNELS]
end
enum ChannelId
Invalid = 0
FrontLeft = 1
FrontRight = 2
FrontCenter = 3
# ...
end
# ...
end
注意:您可能需要手动更新结果文件,因为crystal_lib仍处于试验阶段。