Created
June 18, 2009 13:44
-
-
Save fastman/131907 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "sample.h" | |
/* this does nottin */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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