Skip to content

Instantly share code, notes, and snippets.

@fastman
Created June 18, 2009 13:44
Show Gist options
  • Save fastman/131907 to your computer and use it in GitHub Desktop.
Save fastman/131907 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'ffi'
module E
extend FFI::Library
ffi_lib 'libsample.dylib'
class SampleStruct < FFI::Struct
layout \
:i, :int,
:tab, [:int, 5]
end
end
a = [1, 2, 3, 4, 5]
ptr_1 = FFI::MemoryPointer.new :pointer, E::SampleStruct.size
ptr_2 = FFI::MemoryPointer.new :pointer, E::SampleStruct.size
s1 = E::SampleStruct.new(ptr_1)
s2 = E::SampleStruct.new(ptr_2)
s1[:tab].to_ptr.write_array_of_int(a)
s2[:tab].to_ptr.write_array_of_int(a.reverse)
s1[:tab].to_ptr.read_array_of_int(5).each do |p|
puts p
end
s2[:tab].to_ptr.read_array_of_int(5).each do |p|
puts p
end
CC=gcc
CFLAGS=-Wall -g -ansi -pedantic
OUT=sample
sample:
$(CC) $(CFLAGS) -c sample.c
$(CC) $(CFLAGS) -o $(OUT) sample.o
sample.o:
$(CC) $(CFLAGS) -c sample.c -fPIC -o sample.o
libsample.dylib: sample.o
$(CC) $(CFLAGS) -dynamiclib -shared -soname,[email protected] -o $@ sample.o
lib: libsample.dylib
run: sample
./sample
clean:
rm -rf $(OUT) *.o *.dylib
#include "sample.h"
/* this does nottin */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
typedef struct {
int i;
int tab[5];
} SampleStruct;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment