The way I do it is to make sure all entry points in the C++ file are extern "C"
and create a C wrapper file that calls the C++ funcs which have C linkage.
Then I run extconf.rb for this wrapper file to create a Ruby extension makefile.
I picked most of this up from Pickax I, "Extending Ruby."

Also, for some reason I cannot fathom, I had to replace all C++ I/O with C I/O
routines. After doing that, I was able to compile the C++ file, then compile the
C extension and properly link everything together as long as I remembered to add
-lstdc++ as a link option. I always link to either a .a or a .o so I don't know
how well this applies to a dll.

This was all done with Windows XP / Cygwin / gcc, so my particulars probably do
not apply to your situation, but the overall plan of attack could be applied.

BTW, you wont be able to link to a 'static' function. It would be better to
rewrite it like this:

.cpp file:
extern "C" void greet(char *name) {
    // C++ stuff here

    printf( "Hello %s\n", name );
}

and then I would create a standard .c wrapper file for this, where the
Ruby extension initialization code is put.



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.
> 
> --
> R. Mark Volkmann
> Partner, Object Computing, Inc.