# Static functions, extensions, linkage

**URL:** <https://rubytalk.org/t/static-functions-extensions-linkage/29531>\
**Category:** ruby-talk\
**Created:** [2 August 2006 00:10 UTC](https://rubytalk.org/t/static-functions-extensions-linkage/29531 "2006-08-02T00:10:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [2 August 2006 00:10 UTC](https://rubytalk.org/t/static-functions-extensions-linkage/29531/1 "2006-08-02T00:10:05Z")

</div>

Hi,

How do you get at a static function from within a C extension? I tried  
this snippet where I attempt to use flo\_plus from numeric.c:

/\* foo.c \*/  
#include \<ruby.h\>

extern VALUE flo\_plus(VALUE, VALUE);

static VALUE foo\_test(VALUE x, VALUE y){  
&nbsp;&nbsp;&nbsp;return flo\_plus(x, y);  
}

void Init\_foo(){  
&nbsp;&nbsp;&nbsp;VALUE cFoo = rb\_define\_class("Foo", rb\_cObject);  
&nbsp;&nbsp;&nbsp;rb\_define\_method(cFoo, "test", foo\_test, 2);  
}

# test.rb  
$:unshift Dir.pwd  
require 'foo'  
f = Foo.new  
f.test

This result is:

ruby: symbol lookup error:  
/home/djberge/programming/ruby/extensions/foo.so: undefined symbol:  
flo\_plus

I tried linking explicitly against -lruby but that didn't help.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [2 August 2006 00:28 UTC](https://rubytalk.org/t/static-functions-extensions-linkage/29531/2 "2006-08-02T00:28:54Z")

</div>

Daniel Berger wrote:

> Hi,
> 
> How do you get at a static function from within a C extension? I tried  
> this snippet where I attempt to use flo\_plus from numeric.c:
> 
> /\* foo.c \*/  
> #include \<ruby.h\>
> 
> extern VALUE flo\_plus(VALUE, VALUE);
> 
> static VALUE foo\_test(VALUE x, VALUE y){  
> &nbsp;&nbsp;&nbsp;return flo\_plus(x, y);  
> }
> 
> void Init\_foo(){  
> &nbsp;&nbsp;&nbsp;VALUE cFoo = rb\_define\_class("Foo", rb\_cObject);  
> &nbsp;&nbsp;&nbsp;rb\_define\_method(cFoo, "test", foo\_test, 2);  
> }
> 
> # test.rb  
> $:unshift Dir.pwd  
> require 'foo'  
> f = Foo.new  
> f.test
> 
> This result is:
> 
> ruby: symbol lookup error:  
> /home/djberge/programming/ruby/extensions/foo.so: undefined symbol:  
> flo\_plus
> 
> I tried linking explicitly against -lruby but that didn't help.
> 
> Regards,
> 
> Dan

Where is 'flo\_plus' defined?

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [2 August 2006 04:03 UTC](https://rubytalk.org/t/static-functions-extensions-linkage/29531/3 "2006-08-02T04:03:17Z")

</div>

I could be wrong, but I believe the entire purpose of a static function is to \_not\_ be get at able from other code (sort of like private in ruby).

> **···**
>
> On Aug 1, 2006, at 8:10 PM, Daniel Berger wrote:
> 
> > Hi,
> > 
> > How do you get at a static function from within a C extension? I tried  
> > this snippet where I attempt to use flo\_plus from numeric.c:

---

<div class="post-metadata">

**Author:** ![Vincent\_Fourmond](https://avatars.discourse-cdn.com/v4/letter/v/cdc98d/32.png) [@Vincent\_Fourmond](https://rubytalk.org/u/Vincent_Fourmond)\
**Post date:** [2 August 2006 04:42 UTC](https://rubytalk.org/t/static-functions-extensions-linkage/29531/4 "2006-08-02T04:42:33Z")

</div>

Hello !

> This result is:
> 
> ruby: symbol lookup error:  
> /home/djberge/programming/ruby/extensions/foo.so: undefined symbol:  
> flo\_plus
> 
> I tried linking explicitly against -lruby but that didn't help.

&nbsp;&nbsp;By the way, if you're interested to know which symbols are exported  
from a library, just do

&nbsp;&nbsp;nm -D /usr/lib/library.so

&nbsp;&nbsp;A look to that on my computer shows no references to flo\_plus, only:  
nm -D /usr/lib/libruby1.8.so | grep flo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U flock  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U floor  
0000000000057ef0 T rb\_float\_new

&nbsp;&nbsp;Cheers

&nbsp;&nbsp;Vincent

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [2 August 2006 00:55 UTC](https://rubytalk.org/t/static-functions-extensions-linkage/29531/5 "2006-08-02T00:55:07Z")

</div>

Timothy Hunter wrote:

> Daniel Berger wrote:  
> \> Hi,  
> \>  
> \> How do you get at a static function from within a C extension? I tried  
> \> this snippet where I attempt to use flo\_plus from numeric.c:  
> \>  
> \> /\* foo.c \*/  
> \> #include \<ruby.h\>  
> \>  
> \> extern VALUE flo\_plus(VALUE, VALUE);  
> \>  
> \> static VALUE foo\_test(VALUE x, VALUE y){  
> \> return flo\_plus(x, y);  
> \> }  
> \>  
> \> void Init\_foo(){  
> \> VALUE cFoo = rb\_define\_class("Foo", rb\_cObject);  
> \> rb\_define\_method(cFoo, "test", foo\_test, 2);  
> \> }  
> \>  
> \> # test.rb  
> \> $:unshift Dir.pwd  
> \> require 'foo'  
> \> f = Foo.new  
> \> f.test  
> \>  
> \> This result is:  
> \>  
> \> ruby: symbol lookup error:  
> \> /home/djberge/programming/ruby/extensions/foo.so: undefined symbol:  
> \> flo\_plus  
> \>  
> \> I tried linking explicitly against -lruby but that didn't help.  
> \>  
> \> Regards,  
> \>  
> \> Dan  
> \>  
> \>  
> \>  
> Where is 'flo\_plus' defined?

In numeric.c. Note that it \*builds\* fine, it just doesn't \*run\*.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter2](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Tim\_Hunter2](https://rubytalk.org/u/Tim_Hunter2)\
**Post date:** [2 August 2006 12:50 UTC](https://rubytalk.org/t/static-functions-extensions-linkage/29531/6 "2006-08-02T12:50:10Z")

</div>

Vincent Fourmond wrote:

> &nbsp;&nbsp;By the way, if you're interested to know which symbols are exported  
> from a library, just do
> 
> &nbsp;&nbsp;nm -D /usr/lib/library.so
> 
> &nbsp;&nbsp;A look to that on my computer shows no references to flo\_plus, only:  
> nm -D /usr/lib/libruby1.8.so | grep flo  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U flock  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U floor  
> 0000000000057ef0 T rb\_float\_new

In Ruby, the functions that are intended for use by extensions start with the prefix "rb\_" and are declared in either ruby.h or intern.h. Since flo\_plus doesn't start with "rb\_" it isn't intended to be available to extensions.

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [2 August 2006 01:05 UTC](https://rubytalk.org/t/static-functions-extensions-linkage/29531/7 "2006-08-02T01:05:28Z")

</div>

Daniel Berger wrote:

> Timothy Hunter wrote:  
> &nbsp;&nbsp;
> 
> > Daniel Berger wrote:  
> > &nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > > Hi,
> > > 
> > > How do you get at a static function from within a C extension? I tried  
> > > this snippet where I attempt to use flo\_plus from numeric.c:
> > > 
> > > /\* foo.c \*/  
> > > #include \<ruby.h\>
> > > 
> > > extern VALUE flo\_plus(VALUE, VALUE);
> > > 
> > > static VALUE foo\_test(VALUE x, VALUE y){  
> > > &nbsp;&nbsp;&nbsp;return flo\_plus(x, y);  
> > > }
> > > 
> > > void Init\_foo(){  
> > > &nbsp;&nbsp;&nbsp;VALUE cFoo = rb\_define\_class("Foo", rb\_cObject);  
> > > &nbsp;&nbsp;&nbsp;rb\_define\_method(cFoo, "test", foo\_test, 2);  
> > > }
> > > 
> > > # test.rb  
> > > $:unshift Dir.pwd  
> > > require 'foo'  
> > > f = Foo.new  
> > > f.test
> > > 
> > > This result is:
> > > 
> > > ruby: symbol lookup error:  
> > > /home/djberge/programming/ruby/extensions/foo.so: undefined symbol:  
> > > flo\_plus
> > > 
> > > I tried linking explicitly against -lruby but that didn't help.
> > > 
> > > Regards,
> > > 
> > > Dan
> > 
> > Where is 'flo\_plus' defined?  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > In numeric.c. Note that it \*builds\* fine, it just doesn't \*run\*.
> 
> Regards,
> 
> Dan

I see. Since flo\_plus has static linkage in numeric.c you can't call it from your extension. It builds okay because you supplied a declaration for it, and it links okay because Linux does lazy linking (that is, external symbols aren't resolved until the .so gets loaded), but it won't run because there's no external definition of flo\_plus to match your external reference.

You'll have to use rb\_funcall to call "+".
