# Ruby extension and static function in C++

**URL:** <https://www.ruby-forum.com/t/ruby-extension-and-static-function-in-c/64930>\
**Category:** Ruby\
**Created:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/ruby-extension-and-static-function-in-c/64930 "2006-08-15T14:20:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/ruby-extension-and-static-function-in-c/64930/1 "2006-08-15T14:20:37Z")

</div>

HI,

I am writing an extension for Ruby in C++ and as I have more and more  
functions, I need to separate them into individual C++ source for  
easier maintenance in the long run.

From Programming Ruby, the section on Extending Ruby defines function  
as static, if I continue to use the static keyword and the functions  
are implemented in separate C++ source file, it would not be visible in  
the main ruby method registration file.

How significant is the static keyword when defining functions for use  
as Ruby extension?

I had a quick browse of the ruby-netcdf package and noticed that they  
do not prefix their function with the static keyword.

Anyone has advice?

Cheers

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/ruby-extension-and-static-function-in-c/64930/2 "2006-08-15T14:20:44Z")

</div>

On 8/14/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

As a general rule, I dislike exposing C++ as a library interface. I  
prefer to write a light wrapper in C on top of the C++ functionality-  
much easier to link with other code that way because you have much  
more control over the names that the linker sees. Then, put the Ruby  
wrapper on top of the C wrapper, and all of the Ruby extension  
functions can be static, and it will all fit reasonably into one  
source file (because the extension functions are only making calls to  
another interface).

For an example, look at the extension code in EventMachine (under  
branch version\_0). The Ruby wrapper is in rubymain.cpp. The C wrapper  
is in cmain.cpp. Everything else is C++.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/ruby-extension-and-static-function-in-c/64930/3 "2006-08-15T14:20:55Z")

</div>

Francis C. wrote:

> For an example, look at the extension code in EventMachine (under  
> branch version\_0). The Ruby wrapper is in rubymain.cpp. The C wrapper  
> is in cmain.cpp. Everything else is C++.

Thanks.

Looking at the code

static VALUE t\_add\_oneshot\_timer (VALUE self, VALUE interval)  
{  
const char \*f = evma\_install\_oneshot\_timer (FIX2INT (interval));  
if (!f || !\*f)  
rb\_raise (rb\_eRuntimeError, “no timer”);  
return rb\_str\_new2 (f);  
}

I have a further question, the parameter I get in my call is an array  
e.g.

```
    ["pos",[0, 0, 0],"dir",[1, 0, 1],"custom",[ 0, 1, 1, 2, 2, 2,

```

3, 1, 1]]

```
    they are token-value pair

```

Should I process the array in cmain.cpp or rubymain.cpp

If in cmain.cpp I would need to include “ruby.h” for the definition of  
“VALUE” right?

Cheers

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/ruby-extension-and-static-function-in-c/64930/4 "2006-08-15T14:20:59Z")

</div>

On 8/15/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Should I process the array in cmain.cpp or rubymain.cpp
> 
> If in cmain.cpp I would need to include “ruby.h” for the definition of  
> “VALUE” right?

You should follow whatever conventions make sense for your own code.  
For my  
code, I would answer your question by saying that all of the Ruby stuff  
belongs in the extension wrapper (called rubymain.cpp in my case), so  
your  
extension code should convert the Ruby array to something directly  
accessible from C. And therefore nothing but rubymain.cpp should include  
ruby.h. That especially makes sense if you’re writing an extension to  
wrap  
up already-existing code that knows nothing about Ruby in the first  
place.

EventMachine is completely new code, but it was designed to be  
completely  
independent of Ruby, and if you compile everything except rubymain, you  
get  
a fully-functional C++ library with a C interface that you can use from  
any  
language that can dynamically link code. It’s nice to do things in  
well-defined layers like that.
