# \[FFI\] how to bind to a function with variable number of args

**URL:** <https://www.ruby-forum.com/t/ffi-how-to-bind-to-a-function-with-variable-number-of-args/157932>\
**Category:** Ruby\
**Created:** [January 16, 2009, 6:01pm UTC](https://www.ruby-forum.com/t/ffi-how-to-bind-to-a-function-with-variable-number-of-args/157932 "2009-01-16T18:01:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![keltia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/da6949/32.png) [@keltia](https://www.ruby-forum.com/u/keltia)\
**Post date:** [January 16, 2009, 6:01pm UTC](https://www.ruby-forum.com/t/ffi-how-to-bind-to-a-function-with-variable-number-of-args/157932/1 "2009-01-16T18:01:47Z")

</div>

I’m looking at ruby-ffi 0.2.0 with MRI 1.8.7 (haven’t tried with  
rubinius yet) and I can get to attach to setproctitle(3) defined like  
this:

```
 void
 setproctitle(const char *fmt, ...);

```

* * *

require “ffi”

# 

module LibC  
extend FFI::Library

attach\_function :setproctitle, [:string, :string], []  
end

## LibC.setproctitle(“Ruby: executing %s”, $0)

## gives

## FFI::NotFoundError: Function ‘setproctitle’ not found in [[current process]] from /opt/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib/ffi/library.rb:65:in `attach\_function’

Any idea?

---

<div class="post-metadata">

**Author:** ![keltia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/da6949/32.png) [@keltia](https://www.ruby-forum.com/u/keltia)\
**Post date:** [January 17, 2009, 11:34am UTC](https://www.ruby-forum.com/t/ffi-how-to-bind-to-a-function-with-variable-number-of-args/157932/2 "2009-01-17T11:34:51Z")

</div>

On 16 janv. 09, at 18:00, Ollivier R. wrote:

> I’m looking at ruby-ffi 0.2.0 with MRI 1.8.7 (haven’t tried with  
> rubinius yet) and I can get to attach to setproctitle(3) defined like  
> this:
> 
> ```
> void
> setproctitle(const char *fmt, ...);
> 
> ```

This would be defined like this:

module LibC  
extend FFI::Library  
attach\_function : setproctitle, [:string, :varargs], :void  
end

And then used like this:

LibC.setproctitle("%d-%d", [:string, “foo”, :int, 42])

---

<div class="post-metadata">

**Author:** ![keltia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/da6949/32.png) [@keltia](https://www.ruby-forum.com/u/keltia)\
**Post date:** [January 17, 2009, 11:37am UTC](https://www.ruby-forum.com/t/ffi-how-to-bind-to-a-function-with-variable-number-of-args/157932/3 "2009-01-17T11:37:47Z")

</div>

On 17 janv. 09, at 11:31, Luc H. wrote:

> LibC.setproctitle("%d-%d", [:string, “foo”, :int, 42])

Er, the format string should have obviously been “%s-%d” here, sorry.

---

<div class="post-metadata">

**Author:** ![keltia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/da6949/32.png) [@keltia](https://www.ruby-forum.com/u/keltia)\
**Post date:** [January 19, 2009, 12:28pm UTC](https://www.ruby-forum.com/t/ffi-how-to-bind-to-a-function-with-variable-number-of-args/157932/4 "2009-01-19T12:28:35Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Luc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This would be defined like this:

## The following does not work:

module LibC  
extend FFI::Library

attach\_function :setproctitle, [:string, :varargs], :void  
end

## LibC.setproctitle(“Ruby: executing %s”, [:string, $0])

Same error:  
/opt/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib/ffi/library.rb:65:in  
`attach\_function’: Function ‘setproctitle’ not found in [[current  
process]]  
(FFI::NotFoundError) from proc.rb:12

(OS X, 10.5.6, Ruby from MacPorts)

UPDATE: setproctitle may not be available in OS X. Retrying on FreeBSD  
leads  
to this:

/usr/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib/ffi/types.rb:18:in  
`find_type': Unable to resolve type 'stringproc.rb' (FFI::TypeError) from /usr/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib/ffi/variadic.rb:23:in `call’  
from (eval):3:in `setproctitle’  
from proc.rb:15

---

<div class="post-metadata">

**Author:** ![keltia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/da6949/32.png) [@keltia](https://www.ruby-forum.com/u/keltia)\
**Post date:** [January 19, 2009, 1:53pm UTC](https://www.ruby-forum.com/t/ffi-how-to-bind-to-a-function-with-variable-number-of-args/157932/5 "2009-01-19T13:53:42Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Luc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is the right way to call variadic functions using FFI (remove the  
> []):
> 
> LibC.setproctitle(“Ruby: executing %s”, :string, $0)

… and the winner is you 🙂

47629 ph S+ 0:00.26 ruby: Ruby: executing proc.rb (ruby)

(some redundancy there… 🙂 )

> Sorry about the wrong answers… 😕

No worry, merci.

---

<div class="post-metadata">

**Author:** ![keltia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/da6949/32.png) [@keltia](https://www.ruby-forum.com/u/keltia)\
**Post date:** [January 19, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/ffi-how-to-bind-to-a-function-with-variable-number-of-args/157932/6 "2009-01-19T13:07:33Z")

</div>

On 19 janv. 09, at 12:23, Ollivier R. wrote:

> UPDATE: setproctitle may not be available in OS X.

Right. setproctitle is not available on OS X.

> Retrying on FreeBSD leads  
> to this:
> 
> /usr/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib/ffi/types.rb:18:in  
> `find\_type’: Unable to resolve type ‘stringproc.rb’ (FFI::TypeError)

Yes, and that’s because I need some sleep 😉

This is the right way to call variadic functions using FFI (remove the  
[]):

LibC.setproctitle(“Ruby: executing %s”, :string, $0)

Sorry about the wrong answers… 😕
