[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

C++ DLL example

Mark Volkmann

3/10/2006 9:18:00 PM

Can someone point me to a simple example of calling a C++ function from Ruby?
I know how to do it with SWIG, but I'm trying to learn how to do it
without SWIG.
I'd like to do this under Windows and have the C++ code in a DLL.
No Windows APIs will be invoked. I just want to do something simple in
C++ like this

static void greet(const string& name) {
cout << "Hello " << name << endl;
}

and invoke the greet function from Ruby.

I know how to build a DLL using VC++ 8.
I suspect the issue is that I don't have the right setup in my .cpp file.

--
R. Mark Volkmann
Partner, Object Computing, Inc.


4 Answers

vanekl

3/10/2006 11:56:00 PM

0

Simon Kröger

3/11/2006 11:11:00 AM

0

Hi Mark,

it's not that hard, your cpp file should/could look like this:

---------------------------------------------------------------------
#include "ruby.h"
#include <string>
#include <iostream>

using namespace std;

static void greet(const string& name) {
cout << "Hello " << name << endl;
}

static VALUE rb_greet(VALUE self, VALUE name){
greet(STR2CSTR(name));
return Qnil;
}

void Init_RubyDll() {
rb_define_global_function("greet", (VALUE(*)(ANYARGS))rb_greet, 1);
}
---------------------------------------------------------------------

Add this to a new dll project, add the path to ruby.h to the project
settings and link against msvcrt-ruby18.lib.

Finally you have to export the Init_XXX (XXX has to be equal to the name
of your Dll) function. I usually add a /EXPORT:Init_RubyDll to the
command line options of the linker.

After doing so i could execute

C:\>ruby -e "require 'RubyDll'; greet 'Mark '"
Hello Mark

Hope that helps.

cheers

Simon

p.s.: one gotcha left: The ruby in the one-click-installer is compiled
with vc6, linking to libs from another version might be risky.

Mark Volkmann schrieb:
> Can someone point me to a simple example of calling a C++ function from Ruby?
> I know how to do it with SWIG, but I'm trying to learn how to do it
> without SWIG.
> I'd like to do this under Windows and have the C++ code in a DLL.
> No Windows APIs will be invoked. I just want to do something simple in
> C++ like this
>
> static void greet(const string& name) {
> cout << "Hello " << name << endl;
> }
>
> and invoke the greet function from Ruby.
>
> I know how to build a DLL using VC++ 8.
> I suspect the issue is that I don't have the right setup in my .cpp file.
>
> --
> R. Mark Volkmann
> Partner, Object Computing, Inc.
>
>



Gareth Reeves

3/11/2006 1:58:00 PM

0

Mark Volkmann wrote:
> Can someone point me to a simple example of calling a C++ function from
> Ruby?
> I know how to do it with SWIG, but I'm trying to learn how to do it
> without SWIG.
> I'd like to do this under Windows and have the C++ code in a DLL.
> No Windows APIs will be invoked. I just want to do something simple in
> C++ like this
>
> static void greet(const string& name) {
> cout << "Hello " << name << endl;
> }
>
> and invoke the greet function from Ruby.
>
> I know how to build a DLL using VC++ 8.
> I suspect the issue is that I don't have the right setup in my .cpp
> file.

I had a question on C++ extensions that contained some code examples.
Its here http://www.ruby-...topic....

Gareth


--
Posted via http://www.ruby-....


Mark Volkmann

3/12/2006 3:01:00 PM

0

Thanks for the detailed instructions Simon!

I'm trying to build my DLL with Visual C++ 2005 Express Edition which
is free. I saw your warning about the possibility that this won't work
because the one-click installer is compiled with VC6. I'm not able to
get to the point to test that though. I'm getting a compile error that
says win32.h needs windows.h and it can't find that. I searched my
entire hard drive for windows.h and only found one under my cygwin
directory. I don't want to use that one because I want to find a
solution that doesn't assume cygwin is installed. Any idea why it's
looking for windows.h and where it should be?

On 3/11/06, Simon Kröger <SimonKroeger@gmx.de> wrote:
> Hi Mark,
>
> it's not that hard, your cpp file should/could look like this:
>
> ---------------------------------------------------------------------
> #include "ruby.h"
> #include <string>
> #include <iostream>
>
> using namespace std;
>
> static void greet(const string& name) {
> cout << "Hello " << name << endl;
> }
>
> static VALUE rb_greet(VALUE self, VALUE name){
> greet(STR2CSTR(name));
> return Qnil;
> }
>
> void Init_RubyDll() {
> rb_define_global_function("greet", (VALUE(*)(ANYARGS))rb_greet, 1);
> }
> ---------------------------------------------------------------------
>
> Add this to a new dll project, add the path to ruby.h to the project
> settings and link against msvcrt-ruby18.lib.
>
> Finally you have to export the Init_XXX (XXX has to be equal to the name
> of your Dll) function. I usually add a /EXPORT:Init_RubyDll to the
> command line options of the linker.
>
> After doing so i could execute
>
> C:\>ruby -e "require 'RubyDll'; greet 'Mark '"
> Hello Mark
>
> Hope that helps.
>
> cheers
>
> Simon
>
> p.s.: one gotcha left: The ruby in the one-click-installer is compiled
> with vc6, linking to libs from another version might be risky.
>
> Mark Volkmann schrieb:
> > Can someone point me to a simple example of calling a C++ function from Ruby?
> > I know how to do it with SWIG, but I'm trying to learn how to do it
> > without SWIG.
> > I'd like to do this under Windows and have the C++ code in a DLL.
> > No Windows APIs will be invoked. I just want to do something simple in
> > C++ like this
> >
> > static void greet(const string& name) {
> > cout << "Hello " << name << endl;
> > }
> >
> > and invoke the greet function from Ruby.
> >
> > I know how to build a DLL using VC++ 8.
> > I suspect the issue is that I don't have the right setup in my .cpp file.
> >
> > --
> > R. Mark Volkmann
> > Partner, Object Computing, Inc.
> >
> >
>
>
>


--
R. Mark Volkmann
Partner, Object Computing, Inc.