File: README.md

package info (click to toggle)
ruby-ffi-compiler 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 156 kB
  • sloc: ruby: 556; ansic: 16; makefile: 4
file content (74 lines) | stat: -rw-r--r-- 1,761 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

[ffi-compiler](https://github.com/ffi/ffi-compiler) is a ruby library for automating compilation of native libraries for use with [ffi](https://github.com/ffi/ffi)

To use, define your own ruby->native API using ffi, implement it in C, then use ffi-compiler to compile it.

Example
------
	
###### Directory layout
	lib
	  |- example
	      |- example.rb
	      
	ext
      |- example.c
      |- Rakefile
      
    example.gemspec

###### lib/example/example.rb
	require 'ffi'
	require 'ffi-compiler/loader'
	
	module Example
	  extend FFI::Library
	  ffi_lib FFI::Compiler::Loader.find('example')
	  
	  # example function which takes no parameters and returns long
	  attach_function :example, [], :long
	end

###### ext/example.c
	long
	example(void)
	{
	    return 0xdeadbeef;
	}

###### ext/Rakefile
	require 'ffi-compiler/compile_task'
	
	FFI::Compiler::CompileTask.new('example') do |c|
	  c.have_header?('stdio.h', '/usr/local/include')
	  c.have_func?('puts')
	  c.have_library?('z')
	end

###### example.gemspec
	Gem::Specification.new do |s|
      s.extensions << 'ext/Rakefile'
	  s.name = 'example'
	  s.version = '0.0.1'
	  s.email = 'ffi-example'
	  s.files = %w(example.gemspec) + Dir.glob("{lib,spec,ext}/**/*")
	  s.add_dependency 'rake'
	  s.add_dependency 'ffi-compiler'
	end
    
###### Build gem and install it
	gem build example.gemspec && gem install example-0.0.1.gem
	Successfully built RubyGem
	  Name: example
	  Version: 0.0.1
	  File: example-0.0.1.gem
	Building native extensions.  This could take a while...
	Successfully installed example-0.0.1

###### Test it
	$ irb
	2.0.0dev :001 > require 'example/example'
	 => true 
	2.0.0dev :002 > puts "Example.example=#{Example.example.to_s(16)}"
	Example.example=deadbeef
	 => nil