From: Konrad Meyer Date: 2007-09-22T08:43:06+09:00 Subject: Re: CplusRuby - Gluing C and Ruby --nextPart66148048.BTmV49FagV Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Quoth Konrad Meyer: > Quoth Daniel Berger: > >=20 > > On Sep 21, 1:01 pm, Michael Neumann wrote: > > > Hi all, > > > > > > I am proud to announce my latest project called CplusRuby. > > > Below I pasted it's README for further explanation. > > > Alternatively read the following blog article: > > > > > > http://www.ntecs.de/blog/articles/2007/09/21/cplusruby-gluing-c-and-r= uby > >=20 > > > >=20 > > First patch! > >=20 > > C:\Documents and Settings\djberge\My Documents\My Downloads\Ruby>diff - > > u cplusruby.orig cplusruby.rb > > --- cplusruby.orig Fri Sep 21 15:50:38 2007 > > +++ cplusruby.rb Fri Sep 21 15:49:04 2007 > > @@ -11,6 +11,10 @@ > > # > > # Implements a simple ordered Hash > > # > > +require 'rbconfig' > > +require 'win32/process' if RUBY_PLATFORM.match('mswin') > > +include Config > > + > > class OHash < Hash > > def []=3D(k, v) > > @order ||=3D [] > > @@ -146,20 +150,28 @@ > > dir =3D File.dirname(file) > > mod, ext =3D base.split(".") > >=20 > > + make =3D RUBY_PLATFORM.match('mswin') ? 'nmake' : 'make' > > + > > File.open(file, 'w+') {|f| f << self.generate_code(mod) } > > Dir.chdir(dir) do > > - system("make clean") > > + system("#{make} clean") > > + > > pid =3D fork do > > require 'mkmf' > > $CFLAGS =3D cflags > > $LIBS << (" " + libs) > > create_makefile(mod) > > - exec "make" > > + exec "#{make}" > > end > > _, status =3D Process.waitpid2(pid) > > - raise if status.exitstatus !=3D 0 > > + > > + if RUBY_PLATFORM.match('mswin') > > + raise if status !=3D 0 > > + else > > + raise if status.exitstatus !=3D 0 > > + end > > end > > - require "#{dir}/#{mod}.so" > > + require "#{dir}/#{mod}." + CONFIG['DLEXT'] > > end > >=20 > > def self.generate_code_for_class > >=20 > > Mostly fixes MS Windows issues, but the DLEXT bit also fixes platforms > > that don't generate .so files (OS X, HP-UX). > >=20 > > Regards, > >=20 > > Dan >=20 > Does the C->ruby mapping map between char * and Strings? >=20 > Thanks, > --=20 > Konrad Meyer http://konrad.sobertillnoon.com/ Okay, so I have added the following to DEFAULT_OPTIONS: :int =3D> { :default =3D> '0', :ctype =3D> 'int %s', :ruby2c =3D> '(int)NUM2INT(%s)', :c2ruby =3D> 'INT2NUM(%s)'}, :char_p =3D> { :default =3D> '""', :ctype =3D> 'char *%s', :ruby2c =3D> 'StringValuePtr(%s)', :c2ruby =3D> 'rb_str_new2(%s)'}=20 My code is like this: #---------------------start require 'cplusruby' class BankAccount < CplusRuby property :balance, :int property :name, :char_p method_c :withdraw, %{int dollars}, %{ if (dollars <=3D selfc->balance){ selfc->balance -=3D dollars; return true; } } def initialize end end CplusRuby.evaluate("foo.c", "-Os", "") n =3D BankAccount.new puts n.balance puts n.name #---------------------end When I run it with ruby I get a whole bunch of errors stemming from the fact that it generates C code that uses a type called "BankAccount" without defining it anywhere (struct BankAccount is defined though). Ideas, suggestions, questions? Thanks, =2D-=20 Konrad Meyer http://konrad.sobertillnoon.com/ --nextPart66148048.BTmV49FagV Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBG9Fb6CHB0oCiR2cwRAjEHAKDIVdHFl0cWZnSd25iAtz7WDOhMjgCgmDo9 tgGbQ8qP7q5RtCQ7R6VjMqk= =PjjL -----END PGP SIGNATURE----- --nextPart66148048.BTmV49FagV--