SWIG and char * parameters

I’ve just started to try to use SWIG to wrap a bunch of C functions so I can
call them from Ruby. If I can get this working it will make some testing
much easier, but I’ve come across a strange problem. Functions which take
char pointer arguments seem to pass in NULL.

···

void sayHelloTo(char *name) {
if (NULL == name) {
printf(“Hello %s\n”, name);
}
else {
printf(“Gimme a name!\n”);
}
}

ben% irb
irb(main):001:0> require ‘sample’
=> true
irb(main):002:0> Sample.sayHello
Hello World
=> nil
irb(main):003:0> Sample.sayHelloTo
ArgumentError: wrong # of arguments(0 for 1)
from (irb):3:in `sayHelloTo’
from (irb):3
irb(main):004:0> Sample.sayHelloTo(“Bob”)
Gimme a name!
=> nil

I’ve looked over the documentation on the generic and Ruby-specific parts of
the SWIG site and haven’t found anything talking about this issue. Any tips,
pointers, URLs or comments?

Oh yeah, sorry:

ben% ruby -v
ruby 1.6.8 (2002-12-24) [i386-linux-gnu]

ben% swig -version
SWIG Version 1.3.19
Copyright © 1995-1998
University of Utah and the Regents of the University of California
Copyright © 1998-2002
University of Chicago
Compiled with i386-redhat-linux-g++

(I’ll try with ruby 1.8 when I can get it to compile)

Looks like it does exactly what you told it to. You passed in Bob.
Therefore, NULL != name, and so it says “Gimme a name!”.

Try passing in nil, you’ll get a seg fault :slight_smile:

What you want is NULL != name.

Not SWIG, just C.

HTH,

···

On Jul 25, Ben Giddings wrote:

I’ve just started to try to use SWIG to wrap a bunch of C functions so I can
call them from Ruby. If I can get this working it will make some testing
much easier, but I’ve come across a strange problem. Functions which take
char pointer arguments seem to pass in NULL.

void sayHelloTo(char *name) {
if (NULL == name) {
printf(“Hello %s\n”, name);
}
else {
printf(“Gimme a name!\n”);
}
}

ben% irb
irb(main):001:0> require ‘sample’
=> true
irb(main):002:0> Sample.sayHello
Hello World
=> nil
irb(main):003:0> Sample.sayHelloTo
ArgumentError: wrong # of arguments(0 for 1)
from (irb):3:in `sayHelloTo’
from (irb):3
irb(main):004:0> Sample.sayHelloTo(“Bob”)
Gimme a name!
=> nil

I’ve looked over the documentation on the generic and Ruby-specific parts of
the SWIG site and haven’t found anything talking about this issue. Any tips,
pointers, URLs or comments?


---------------------------------------------- | --------------------------
Brett Williams |
---------------------------------------------- | --------------------------

D’OH! Now that Brett has pointed out why my oversimplified example was
failing I’ve gone back and found out the problem was more subtle than I
thought:

When I name a function “connect” it behaves differently than if I name it
"connectTo". As far as I’m aware, connect isn’t a reserved word in either
language, and no warnings are generated when I compile this.

Incidentally, I’m doing this to try to make Ruby an RPC client. Is there a
better way of doing this, other than SWIG? The only RPC<->Ruby connections
I’ve found are for XML-RPC.

int connectTo(char *host) {
printf(“Connecting to %s\n”, host);
global_client_ptr = clnt_create (host, MAXWELLPROG, V1, “udp”);
if (global_client_ptr == NULL) {
clnt_pcreateerror (host);
return 0;
}
return 1;
}

int connect(char *host) {
printf(“Connecting to %s\n”, host);
global_client_ptr = clnt_create (host, MAXWELLPROG, V1, “udp”);
if (global_client_ptr == NULL) {
clnt_pcreateerror (host);
return 0;
}
return 1;
}

···

irb(main):001:0> require ‘rpc’
=> true
irb(main):002:0> Rpc.sayHello
Hello World
=> nil
irb(main):003:0> Rpc.connect(“localhost”)
=> -1
irb(main):004:0> Rpc.connectTo(“localhost”)
Connecting to localhost
=> 1
irb(main):005:0> Rpc.disconnect
=> nil
irb(main):006:0> Rpc.connectTo(“localhost”)
Connecting to localhost
=> 1
irb(main):007:0> Rpc.connect(“localhost”)
=> -1
irb(main):008:0> Rpc.disconnect
=> nil

Thanks again,

Ben

I am reading this article :
http://www.infoworld.com/article/03/03/21/12stratdev_1.html

and this messages:

http://lists.ximian.com/archives/public/mono-list/2003-May/013818.html

I would like to see Ruby in the Mono Project, what do you think (matz)?

···


Enrique Meza emeza@debianmexico.org
Jarabe Soft

In Message-Id: 200307241723.41949.ben@thingmagic.com
Ben Giddings ben@thingmagic.com writes:

When I name a function “connect” it behaves differently than if I name it
“connectTo”. As far as I’m aware, connect isn’t a reserved word in either
language, and no warnings are generated when I compile this.

Maybe missing the point but…

  1. Are you sure SWIG wraps your version of connect()?
  2. Redefine a system call / standard library function such as
    connect() is in general a bad idea.

Hope this helps.

···


kjana@dm4lab.to July 25, 2003
Speech is silver, silence is golden.