[GSoC] Header Generation for C/C++
bachmeier
no at spam.net
Tue Jul 16 19:10:41 UTC 2019
On Tuesday, 16 July 2019 at 13:16:50 UTC, Eduard Staniloiu wrote:
> So, by running,
> `dmd -HCf=ab.h -HCd=mypath/ a.d b.d` will write the generated
> header in `mypath/ab.h`, relative to the current directory.
Will it be possible to extend this for other languages? That
would be a killer application. For instance
dmd -HRuby=ab.rb -HCd=mypath/ a.d b.d
goes a step further, converting what would have been the
generated C header to the file ab.rb containing (taken from
https://github.com/ffi/ffi/wiki/Examples)
module ab
extend FFI::Library
ffi_lib "path/to/ab.so"
attach_function :calculate_something, [:int, :float], :double
attach_function :error_code, [], :int # note empty array for
functions taking zero arguments
attach_function :create_object, [:string], :pointer
attach_function :calculate_something_else, [:double, :pointer],
:double
attach_function :free_object, [:pointer], :void
end
Then the Ruby script would call ab.rb as
require 'ffi'
require 'ab'
c = ab.calculate_something(42, 98.6) # note FFI handles literals
just fine
if ( (errcode = ab.error_code()) != 0)
puts "error calculating something: #{errcode}"
exit 1
end
objptr = ab.create_object("my object") # note FFI handles string
literals as well
d = ab.calculate_something_else(c, objptr)
ab.free_object(objptr)
puts "calculated #{d}"
More information about the Digitalmars-d
mailing list