# C++ DLL example

**URL:** <https://rubytalk.org/t/c-dll-example/25645>\
**Category:** ruby-talk\
**Created:** [10 March 2006 21:18 UTC](https://rubytalk.org/t/c-dll-example/25645 "2006-03-10T21:18:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Volkmann1](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Mark\_Volkmann1](https://rubytalk.org/u/Mark_Volkmann1)\
**Post date:** [10 March 2006 21:18 UTC](https://rubytalk.org/t/c-dll-example/25645/1 "2006-03-10T21:18:13Z")

</div>

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) {  
&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Lou\_Vanek](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@Lou\_Vanek](https://rubytalk.org/u/Lou_Vanek)\
**Post date:** [10 March 2006 23:56 UTC](https://rubytalk.org/t/c-dll-example/25645/2 "2006-03-10T23:56:10Z")

</div>

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) {  
&nbsp;&nbsp;&nbsp;&nbsp;// C++ stuff here

&nbsp;&nbsp;&nbsp;&nbsp;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) {  
> > &nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Simon\_Kroger](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Simon\_Kroger](https://rubytalk.org/u/Simon_Kroger)\
**Post date:** [11 March 2006 11:10 UTC](https://rubytalk.org/t/c-dll-example/25645/3 "2006-03-11T11:10:30Z")

</div>

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) {  
> &nbsp;&nbsp;cout \<\< "Hello " \<\< name \<\< endl;  
> }
> 
> static VALUE rb\_greet(VALUE self, VALUE name){  
> &nbsp;&nbsp;greet(STR2CSTR(name));  
> &nbsp;&nbsp;return Qnil;  
> }
> 
> void Init\_RubyDll() {  
> &nbsp;&nbsp;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) {  
> > &nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Gareth\_Reeves](https://avatars.discourse-cdn.com/v4/letter/g/2bfe46/32.png) [@Gareth\_Reeves](https://rubytalk.org/u/Gareth_Reeves)\
**Post date:** [11 March 2006 13:58 UTC](https://rubytalk.org/t/c-dll-example/25645/4 "2006-03-11T13:58:08Z")

</div>

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) {  
> &nbsp;&nbsp;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 [C++ extension - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/57404#new%5C).

Gareth

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Mark\_Volkmann1](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Mark\_Volkmann1](https://rubytalk.org/u/Mark_Volkmann1)\
**Post date:** [12 March 2006 15:01 UTC](https://rubytalk.org/t/c-dll-example/25645/5 "2006-03-12T15:01:11Z")

</div>

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) {  
> > &nbsp;&nbsp;cout \<\< "Hello " \<\< name \<\< endl;  
> > }
> > 
> > static VALUE rb\_greet(VALUE self, VALUE name){  
> > &nbsp;&nbsp;greet(STR2CSTR(name));  
> > &nbsp;&nbsp;return Qnil;  
> > }
> > 
> > void Init\_RubyDll() {  
> > &nbsp;&nbsp;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.
