# Calling c++ from Ruby

**URL:** <https://www.ruby-forum.com/t/calling-c-from-ruby/142713>\
**Category:** Ruby\
**Created:** [July 10, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/calling-c-from-ruby/142713 "2008-07-10T18:25:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [July 10, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/calling-c-from-ruby/142713/1 "2008-07-10T18:25:29Z")

</div>

Hi,

I wrote this question on the newsgroup yesterday, but I didn’t see it  
actually get posted. Here it goes again. I’m trying to call a c++  
function from Ruby and I’m getting some errors compiling the c++  
part.

Here is my code:  
#include “ruby.h”  
#include   
#include \<stdarg.h\>  
VALUE MyTest = Qnil;  
extern “C” VALUE create\_project(char \*c, …)  
{  
va\_list args;  
va\_start(args, c);  
}  
extern “C” void Init\_mytest()  
{  
MyTest = rb\_define\_module(“MyTest”);  
rb\_define\_method(MyTest, “create\_project”, &create\_project,  
1);  
}

And here is the error:  
MyTest.cpp: In function ‘void Init\_mytest()’:  
MyTest.cpp:16: error: invalid conversion from ‘VALUE (_)(char_, …)’  
to ‘VALUE (_)(…)’  
MyTest.cpp:16: error: initializing argument 3 of ‘void  
rb\_define\_method(VALUE, const char_, VALUE (\*)(…), int)’

If I don’t put “char \*c” in the arguments of create\_project, it  
compiles and works just fine. But if I don’t have it, how can I get  
the arguments?  
I realize this is more of a c++ question, but if anyone has any  
simple, fully working example of calling c++ code from Ruby, I would  
like to see it.

Thanks,  
Tiberiu

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [July 10, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/calling-c-from-ruby/142713/2 "2008-07-10T20:09:08Z")

</div>

Dear Tiberiu,

I am not quite sure about your question, but I’d like to suggest this  
link for Ruby/C++  
integration:

[http://rice.rubyforge.org/](http://rice.rubyforge.org/)

There is also a gem based on Rice that promises to wrap whole C++  
libraries in Ruby,

[http://rubyforge.org/projects/rbplusplus](http://rubyforge.org/projects/rbplusplus) .

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [July 10, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/calling-c-from-ruby/142713/3 "2008-07-10T19:50:23Z")

</div>

On Thursday 10 July 2008, Mr\_Tibs wrote:

> #include \<stdarg.h\>  
> 1);  
> compiles and works just fine. But if I don’t have it, how can I get  
> the arguments?  
> I realize this is more of a c++ question, but if anyone has any  
> simple, fully working example of calling c++ code from Ruby, I would  
> like to see it.
> 
> Thanks,  
> Tiberiu

I don’t know whether it has to do with the error you’re getting, but I  
think  
your create\_project method is conceptually wrong, because if it should  
be  
called from ruby, all arguments will be passed to it as VALUE s, not as  
char  
or int or any other C type (this is because all ruby objects, on the C  
side  
are of type VALUE).

Also, if you want to create a method which can be called with any number  
of  
arguments from the ruby side, I think you should define it this way:

VALUE create\_project(int argc, VALUE \* argv, VALUE self)  
{  
}

extern “C” void Init\_mytest(){  
MyTest = rb\_define\_module(“MyTest”);  
rb\_define\_method(MyTest, “create\_project”, &create\_project, -1);  
}

The argc parameter is the number of arguments passed to the method (and  
yes,  
it’s an exception to the rule I spoke of above about the arguments  
always  
having to be of type VALUE). argv is an array of VALUEs representing the  
arguments passed to the method and self is… self. To access the  
arguments of  
the method, you iterate on argv:

for (int i = 0; i \< argc; i++){  
// do something with argv[i]  
}

If some of the parameters of your method are required and some other  
optional,  
you can use the rb\_scan\_args function, which manages them for you.

For more information, have a look at the chapter about extending ruby in  
the  
pickaxe (for the free online edition, the chapter is at  
[Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html)), at the  
Doxygen documentation of the ruby C api and, in particular, to the  
section on  
functions starting with the letter R  
([http://www.ruby-doc.org/doxygen/current/globals\_func.html#index\_r](http://www.ruby-doc.org/doxygen/current/globals_func.html#index_r)).  
This part  
of the api contains a number of methods starting with rb\_, which  
correspond  
(more or less) directly to ruby methods. Unfortunately, there’s no  
description  
of the methods, but you can get an idea of what they do looking at their  
name.  
Lastly, you can look at the source code of ruby itself (that’s how I  
found the  
information I gave you above). There are C files for the core classes  
(array.c, object.c and so on). All the calls to rb\_define\_method are at  
the  
end of the corresponding files, so you can quickly find out which C  
function  
corresponds to which ruby method. You can download the ruby source code  
from  
[http://www.ruby-lang.org](http://www.ruby-lang.org) or browse the svn repository online at  
[http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/tags/](http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/tags/)

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [July 24, 2008, 4:30am UTC](https://www.ruby-forum.com/t/calling-c-from-ruby/142713/4 "2008-07-24T04:30:31Z")

</div>

Mr\_Tibs wrote:

> Hi,
> 
> I wrote this question on the newsgroup yesterday, but I didn’t see it  
> actually get posted. Here it goes again. I’m trying to call a c++  
> function from Ruby and I’m getting some errors compiling the c++  
> part.
> 
> Here is my code:  
> #include “ruby.h”  
> #include   
> #include \<stdarg.h\>  
> VALUE MyTest = Qnil;  
> extern “C” VALUE create\_project(char \*c, …)  
> {  
> va\_list args;  
> va\_start(args, c);  
> }  
> extern “C” void Init\_mytest()  
> {  
> MyTest = rb\_define\_module(“MyTest”);  
> rb\_define\_method(MyTest, “create\_project”, &create\_project,  
> 1);  
> }
> 
> And here is the error:  
> MyTest.cpp: In function ï¿½void Init\_mytest()ï¿½:  
> MyTest.cpp:16: error: invalid conversion from ï¿½VALUE (_)(char_, …)ï¿½  
> to ï¿½VALUE (_)(…)ï¿½  
> MyTest.cpp:16: error: initializing argument 3 of ï¿½void  
> rb\_define\_method(VALUE, const char_, VALUE (\*)(…), int)ï¿½
> 
> If I don’t put “char \*c” in the arguments of create\_project, it  
> compiles and works just fine. But if I don’t have it, how can I get  
> the arguments?  
> I realize this is more of a c++ question, but if anyone has any  
> simple, fully working example of calling c++ code from Ruby, I would  
> like to see it.
> 
> Thanks,  
> Tiberiu

Hi,

I think you meant to use rb\_define\_singleton\_method, instead of  
rb\_define\_method.

Also, the method signature of your C/C++ function is important. If you  
want to use variable number of arguments, it needs to look something  
like this:

VALUE your\_method\_name(int argc, VALUE \*argv, VALUE klass)

Also, when using C++ you need to cast to correct types… so, in your  
case you could fix the compile time error by casting:

rb\_define\_method(MyTest, “create\_project”, (VALUE  
(\*)(…))&create\_project, -1);

Also, when using variable arguments you need to pass a -1, since it’s an  
unknown number of arguments. Ruby also has it’s own set of methods to  
use to access the arguments.

Have a look at rb\_scan\_args,

> **[O'Reilly Media - Technology and Business Training](https://www.oreilly.com)**
>
> Gain technology and business knowledge and hone your skills with learning resources created and curated by O'Reilly's experts: live online training, video, books, our platform has content from 200+ of the world's best publishers.

Hope this helps,  
-Todd
