[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

C extensions question

Jared Richardson

3/23/2006 2:42:00 AM

Hi all,

I haven't done any "real" work in C in nearly 10 years and I'm still
learning Ruby, so it's quite possible that I'm just doing something dumb,
but I'm stuck here. I'm trying to call Ingres database code from Ruby, so
I'm writing some C extensions.

I have Ruby calling into C code. Very easy and very nice.

I have Ingres sample code (which is C) compiling and running fine.

However, when I try to invoke Ingres code from any of my C code and run it
(from Ruby), I get a seg fault.

It feels like Ruby can't see the Ingres libraries but the code samples don't
seem to having any problems. I suspect I'm missing something simple...

Do you need to do any special wrapping or declaring of code that isn't
touched from within your Ruby? I've been through the Pickaxe book and
several online samples/tutorials and I don't see that it's needed... but
then again, my code isn't running. :)

Here's my code as it is now... any hints or clues?

#include "ruby.h"
# include <stdio.h>
# include <iiapi.h>

static VALUE cIngres;

static VALUE ii_exec(VALUE self) {
printf("\n\nExec was run from within the Ruby code \n\n");
IIAPI_INITPARM initParm;
printf("one\n");
IIapi_initialize( &initParm );
printf("two\n");
return Qnil;
}

void Init_Ingres() {
cIngres = rb_define_class("Ingres", rb_cObject);
rb_define_method(cIngres, "exec", ii_exec, 0);
}


Thanks!

Jared
http://jaredrich...


6 Answers

Zeng Ke

3/23/2006 3:18:00 AM

0

You'd better make the Makefile using extconf.rb, in which you can
specify the library path and include path, as is told in the rubybook.


Jared Richardson wrote:

>Hi all,
>
>I haven't done any "real" work in C in nearly 10 years and I'm still
>learning Ruby, so it's quite possible that I'm just doing something dumb,
>but I'm stuck here. I'm trying to call Ingres database code from Ruby, so
>I'm writing some C extensions.
>
>I have Ruby calling into C code. Very easy and very nice.
>
>I have Ingres sample code (which is C) compiling and running fine.
>
>However, when I try to invoke Ingres code from any of my C code and run it
>(from Ruby), I get a seg fault.
>
>It feels like Ruby can't see the Ingres libraries but the code samples don't
>seem to having any problems. I suspect I'm missing something simple...
>
>Do you need to do any special wrapping or declaring of code that isn't
>touched from within your Ruby? I've been through the Pickaxe book and
>several online samples/tutorials and I don't see that it's needed... but
>then again, my code isn't running. :)
>
>Here's my code as it is now... any hints or clues?
>
>#include "ruby.h"
># include <stdio.h>
># include <iiapi.h>
>
>static VALUE cIngres;
>
>static VALUE ii_exec(VALUE self) {
> printf("\n\nExec was run from within the Ruby code \n\n");
> IIAPI_INITPARM initParm;
> printf("one\n");
> IIapi_initialize( &initParm );
> printf("two\n");
> return Qnil;
>}
>
>void Init_Ingres() {
> cIngres = rb_define_class("Ingres", rb_cObject);
> rb_define_method(cIngres, "exec", ii_exec, 0);
>}
>
>
>Thanks!
>
>Jared
>http://jaredrich...
>
>
>
>
>
>---- X-Spam-Report ----
>
>No, hits=-1.4 required=5.0
>
> 0.1 RCVD_BY_IP Received by mail server with no name
> -2.6 BAYES_00 BODY: Bayesian spam probability is 0 to 1%
> [score: 0.0000]
> 1.1 PRIORITY_NO_NAME Message has priority, but no X-Mailer/User-Agent
>
>
>
>



Jared Richardson

3/23/2006 4:30:00 AM

0

Good point. I should've mentioned that I am already doing that.


"Zeng Ke" <zengke@ce-lab.net> wrote in message
news:442214D4.7050602@ce-lab.net...
> You'd better make the Makefile using extconf.rb, in which you can specify
> the library path and include path, as is told in the rubybook.
>
>
> Jared Richardson wrote:
>
>>Hi all,
>>
>>I haven't done any "real" work in C in nearly 10 years and I'm still
>>learning Ruby, so it's quite possible that I'm just doing something dumb,
>>but I'm stuck here. I'm trying to call Ingres database code from Ruby, so
>>I'm writing some C extensions.
>>
>>I have Ruby calling into C code. Very easy and very nice.
>>
>>I have Ingres sample code (which is C) compiling and running fine.
>>
>>However, when I try to invoke Ingres code from any of my C code and run it
>>(from Ruby), I get a seg fault.
>>
>>It feels like Ruby can't see the Ingres libraries but the code samples
>>don't seem to having any problems. I suspect I'm missing something
>>simple...
>>
>>Do you need to do any special wrapping or declaring of code that isn't
>>touched from within your Ruby? I've been through the Pickaxe book and
>>several online samples/tutorials and I don't see that it's needed... but
>>then again, my code isn't running. :)
>>
>>Here's my code as it is now... any hints or clues?
>>
>>#include "ruby.h"
>># include <stdio.h>
>># include <iiapi.h>
>>
>>static VALUE cIngres;
>>
>>static VALUE ii_exec(VALUE self) {
>> printf("\n\nExec was run from within the Ruby code \n\n");
>> IIAPI_INITPARM initParm;
>> printf("one\n");
>> IIapi_initialize( &initParm );
>> printf("two\n");
>> return Qnil;
>>}
>>
>>void Init_Ingres() {
>> cIngres = rb_define_class("Ingres", rb_cObject);
>> rb_define_method(cIngres, "exec", ii_exec, 0);
>>}
>>
>>
>>Thanks!
>>
>>Jared
>>http://jaredrich...
>>
>>
>>
>>
>>---- X-Spam-Report ----
>>
>>No, hits=-1.4 required=5.0
>>
>> 0.1 RCVD_BY_IP Received by mail server with no name
>> -2.6 BAYES_00 BODY: Bayesian spam probability is 0 to 1%
>> [score: 0.0000]
>> 1.1 PRIORITY_NO_NAME Message has priority, but no
>> X-Mailer/User-Agent
>>
>>
>>
>
>
>


Gerardo Santana Gómez Garrido

3/23/2006 6:36:00 AM

0

Hello Jared.

I'm not familiar with Ingres, but according to

http://www.ingres.com/downloa...

and this

http://www.rubycentral.com/book/ext...

I'd write it this way:

-------------- >8 --------------
#include "ruby.h"
#include <iiapi.h>

static VALUE rb_cIngres;

static void
ingres_free(void *p)
{
IIAPI_RELENVPARM relEnvParm;
IIAPI_TERMPARM termParm;

relEnvParm.re_envHandle = p;
IIapi_releaseEnv(&relEnvParm);
/* test return status */
IIapi_terminate(&termParm);
/* test return status */
}

static VALUE
ingres_alloc(VALUE klass)
{
IIAPI_INITPARM initParm;
/*
* initialize initParm structure here
* before passing it along to IIapi_initialize
*/
IIapi_initialize(&initParm);
if (initParm.in_status != IIAPI_ST_SUCCESS) {
initParm.in_envHandle = NULL;
/* do something else probably */
}
return Data_Wrap_Struct(klass, 0, ingres_free, initParm.in_envHandle);
}

static VALUE
ingres_init(VALUE self)
{
/* Initialization stuff */
return self;
}

void Init_ingres(void) {
rb_cIngres = rb_define_class("Ingres", rb_cObject);
rb_define_method(rb_cIngres, "initialize", ingres_init, 0);
}
-------------- 8< --------------


But I can't test it.

2006/3/22, Jared Richardson <jaredRMV-richaTHSrdson@nc.rr.com>:
> Hi all,
>
> I haven't done any "real" work in C in nearly 10 years and I'm still
> learning Ruby, so it's quite possible that I'm just doing something dumb,
> but I'm stuck here. I'm trying to call Ingres database code from Ruby, so
> I'm writing some C extensions.
>
> I have Ruby calling into C code. Very easy and very nice.
>
> I have Ingres sample code (which is C) compiling and running fine.
>
> However, when I try to invoke Ingres code from any of my C code and run it
> (from Ruby), I get a seg fault.
>
> It feels like Ruby can't see the Ingres libraries but the code samples don't
> seem to having any problems. I suspect I'm missing something simple...
>
> Do you need to do any special wrapping or declaring of code that isn't
> touched from within your Ruby? I've been through the Pickaxe book and
> several online samples/tutorials and I don't see that it's needed... but
> then again, my code isn't running. :)
>
> Here's my code as it is now... any hints or clues?
>
> #include "ruby.h"
> # include <stdio.h>
> # include <iiapi.h>
>
> static VALUE cIngres;
>
> static VALUE ii_exec(VALUE self) {
> printf("\n\nExec was run from within the Ruby code \n\n");
> IIAPI_INITPARM initParm;
> printf("one\n");
> IIapi_initialize( &initParm );
> printf("two\n");
> return Qnil;
> }
>
> void Init_Ingres() {
> cIngres = rb_define_class("Ingres", rb_cObject);
> rb_define_method(cIngres, "exec", ii_exec, 0);
> }
>
>
> Thanks!
>
> Jared
> http://jaredrich...
>
>
>
>


--
Gerardo Santana
"Between individuals, as between nations, respect for the rights of
others is peace" - Don Benito Juárez
http://santanatechnotes.blo...


Jared Richardson

3/23/2006 12:44:00 PM

0

My original code was written just like that but I cut it down for the
newsgroup posting. :)

Thanks!


"Gerardo Santana Gómez Garrido" <gerardo.santana@gmail.com> wrote in message
news:b06e22130603222235r5db97687l@mail.gmail.com...
Hello Jared.

I'm not familiar with Ingres, but according to

http://www.ingres.com/downloa...

and this

http://www.rubycentral.com/book/ext...

I'd write it this way:

-------------- >8 --------------
#include "ruby.h"
#include <iiapi.h>

static VALUE rb_cIngres;

static void
ingres_free(void *p)
{
IIAPI_RELENVPARM relEnvParm;
IIAPI_TERMPARM termParm;

relEnvParm.re_envHandle = p;
IIapi_releaseEnv(&relEnvParm);
/* test return status */
IIapi_terminate(&termParm);
/* test return status */
}

static VALUE
ingres_alloc(VALUE klass)
{
IIAPI_INITPARM initParm;
/*
* initialize initParm structure here
* before passing it along to IIapi_initialize
*/
IIapi_initialize(&initParm);
if (initParm.in_status != IIAPI_ST_SUCCESS) {
initParm.in_envHandle = NULL;
/* do something else probably */
}
return Data_Wrap_Struct(klass, 0, ingres_free, initParm.in_envHandle);
}

static VALUE
ingres_init(VALUE self)
{
/* Initialization stuff */
return self;
}

void Init_ingres(void) {
rb_cIngres = rb_define_class("Ingres", rb_cObject);
rb_define_method(rb_cIngres, "initialize", ingres_init, 0);
}
-------------- 8< --------------


But I can't test it.

2006/3/22, Jared Richardson <jaredRMV-richaTHSrdson@nc.rr.com>:
> Hi all,
>
> I haven't done any "real" work in C in nearly 10 years and I'm still
> learning Ruby, so it's quite possible that I'm just doing something dumb,
> but I'm stuck here. I'm trying to call Ingres database code from Ruby, so
> I'm writing some C extensions.
>
> I have Ruby calling into C code. Very easy and very nice.
>
> I have Ingres sample code (which is C) compiling and running fine.
>
> However, when I try to invoke Ingres code from any of my C code and run it
> (from Ruby), I get a seg fault.
>
> It feels like Ruby can't see the Ingres libraries but the code samples
> don't
> seem to having any problems. I suspect I'm missing something simple...
>
> Do you need to do any special wrapping or declaring of code that isn't
> touched from within your Ruby? I've been through the Pickaxe book and
> several online samples/tutorials and I don't see that it's needed... but
> then again, my code isn't running. :)
>
> Here's my code as it is now... any hints or clues?
>
> #include "ruby.h"
> # include <stdio.h>
> # include <iiapi.h>
>
> static VALUE cIngres;
>
> static VALUE ii_exec(VALUE self) {
> printf("\n\nExec was run from within the Ruby code \n\n");
> IIAPI_INITPARM initParm;
> printf("one\n");
> IIapi_initialize( &initParm );
> printf("two\n");
> return Qnil;
> }
>
> void Init_Ingres() {
> cIngres = rb_define_class("Ingres", rb_cObject);
> rb_define_method(cIngres, "exec", ii_exec, 0);
> }
>
>
> Thanks!
>
> Jared
> http://jaredrich...
>
>
>
>


--
Gerardo Santana
"Between individuals, as between nations, respect for the rights of
others is peace" - Don Benito Juárez
http://santanatechnotes.blo...



Jared Richardson

3/25/2006 1:48:00 AM

0

I finally got this thing working so I thought I'd share the answer, even
though it was a simple problem (once I found it.) :)

I was already generating my Makefile with extconf.rb but I hadn't gotten it
to answer "yes" to any of the have_library or find_libary calls. I did have
it working for have_header. I should've focused on that have_library that
answered "no", but I thought that since the original C samples could run
with the libraries in the LD_LIBRARY_PATH environment variable that the Ruby
code would be okay. It wasn't. :)

It turns out that I wasn't using the right library name. The include was
called "iiapi.h" and the compiled library was named libiiapi.a (static) and
libiiapi.1.so (dynamic). However the name of the library seems to be
"ingres". I'm pretty sure I tried using "ingres" early in the debugging
cycle, but it appears that either I didn't or that I had the environment
messed up at the time.

I haven't done anything "real" with the resulting code yet, but I can call
into the ingres library without a segmentation fault, so I'm going to call
it good and go take a break. :)

I'm pasting in my extconf.rb for future reference.

-----
require 'mkmf'

dir_config("ingres")
have_header("iiapi.h")
have_library("ingres")

$CFLAGS = " -g -Dint_lnx -O "
incdir = ENV["II_SYSTEM"] + "/ingres/files"
$CFLAGS += "-I#{incdir}"
libdir = ENV["II_SYSTEM"] + "/ingres/lib"
$LDFLAGS += "-L#{libdir}"
$LDFLAGS += " -lingres -lpthread -lm -lc -lcrypt -ldl -lgcc_s"

create_makefile("Ingres")
-----

Thanks for the suggestions and help.

Jared
http://jaredrich...


--------------------------
--- Original Message ----
--------------------------


Hi all,

I haven't done any "real" work in C in nearly 10 years and I'm still
learning Ruby, so it's quite possible that I'm just doing something dumb,
but I'm stuck here. I'm trying to call Ingres database code from Ruby, so
I'm writing some C extensions.

I have Ruby calling into C code. Very easy and very nice.

I have Ingres sample code (which is C) compiling and running fine.

However, when I try to invoke Ingres code from any of my C code and run it
(from Ruby), I get a seg fault.

It feels like Ruby can't see the Ingres libraries but the code samples don't
seem to having any problems. I suspect I'm missing something simple...

Do you need to do any special wrapping or declaring of code that isn't
touched from within your Ruby? I've been through the Pickaxe book and
several online samples/tutorials and I don't see that it's needed... but
then again, my code isn't running. :)

Here's my code as it is now... any hints or clues?

#include "ruby.h"
# include <stdio.h>
# include <iiapi.h>

static VALUE cIngres;

static VALUE ii_exec(VALUE self) {
printf("\n\nExec was run from within the Ruby code \n\n");
IIAPI_INITPARM initParm;
printf("one\n");
IIapi_initialize( &initParm );
printf("two\n");
return Qnil;
}

void Init_Ingres() {
cIngres = rb_define_class("Ingres", rb_cObject);
rb_define_method(cIngres, "exec", ii_exec, 0);
}


Thanks!

Jared
http://jaredrich...



Jared Richardson

3/25/2006 1:58:00 AM

0

Also, I forgot to add, in order for this to work, invoke with these options:

ruby -r mkmf
xtconf.rb --with-ingres-include="/opt/Ingres/IngresII/ingres/files/" --with-ingres-lib="/opt/Ingres/IngresII/ingres/lib/"


"Jared Richardson" <jaredRMV-richaTHSrdson@nc.rr.com> wrote in message
news:Yi1Vf.44009$915.14388@southeast.rr.com...
>I finally got this thing working so I thought I'd share the answer, even
>though it was a simple problem (once I found it.) :)
>
> I was already generating my Makefile with extconf.rb but I hadn't gotten
> it to answer "yes" to any of the have_library or find_libary calls. I did
> have it working for have_header. I should've focused on that have_library
> that answered "no", but I thought that since the original C samples could
> run with the libraries in the LD_LIBRARY_PATH environment variable that
> the Ruby code would be okay. It wasn't. :)
>
> It turns out that I wasn't using the right library name. The include was
> called "iiapi.h" and the compiled library was named libiiapi.a (static)
> and libiiapi.1.so (dynamic). However the name of the library seems to be
> "ingres". I'm pretty sure I tried using "ingres" early in the debugging
> cycle, but it appears that either I didn't or that I had the environment
> messed up at the time.
>
> I haven't done anything "real" with the resulting code yet, but I can call
> into the ingres library without a segmentation fault, so I'm going to call
> it good and go take a break. :)
>
> I'm pasting in my extconf.rb for future reference.
>
> -----
> require 'mkmf'
>
> dir_config("ingres")
> have_header("iiapi.h")
> have_library("ingres")
>
> $CFLAGS = " -g -Dint_lnx -O "
> incdir = ENV["II_SYSTEM"] + "/ingres/files"
> $CFLAGS += "-I#{incdir}"
> libdir = ENV["II_SYSTEM"] + "/ingres/lib"
> $LDFLAGS += "-L#{libdir}"
> $LDFLAGS += " -lingres -lpthread -lm -lc -lcrypt -ldl -lgcc_s"
>
> create_makefile("Ingres")
> -----
>
> Thanks for the suggestions and help.
>
> Jared
> http://jaredrich...
>
>
> --------------------------
> --- Original Message ----
> --------------------------
>
>
> Hi all,
>
> I haven't done any "real" work in C in nearly 10 years and I'm still
> learning Ruby, so it's quite possible that I'm just doing something dumb,
> but I'm stuck here. I'm trying to call Ingres database code from Ruby, so
> I'm writing some C extensions.
>
> I have Ruby calling into C code. Very easy and very nice.
>
> I have Ingres sample code (which is C) compiling and running fine.
>
> However, when I try to invoke Ingres code from any of my C code and run it
> (from Ruby), I get a seg fault.
>
> It feels like Ruby can't see the Ingres libraries but the code samples
> don't
> seem to having any problems. I suspect I'm missing something simple...
>
> Do you need to do any special wrapping or declaring of code that isn't
> touched from within your Ruby? I've been through the Pickaxe book and
> several online samples/tutorials and I don't see that it's needed... but
> then again, my code isn't running. :)
>
> Here's my code as it is now... any hints or clues?
>
> #include "ruby.h"
> # include <stdio.h>
> # include <iiapi.h>
>
> static VALUE cIngres;
>
> static VALUE ii_exec(VALUE self) {
> printf("\n\nExec was run from within the Ruby code \n\n");
> IIAPI_INITPARM initParm;
> printf("one\n");
> IIapi_initialize( &initParm );
> printf("two\n");
> return Qnil;
> }
>
> void Init_Ingres() {
> cIngres = rb_define_class("Ingres", rb_cObject);
> rb_define_method(cIngres, "exec", ii_exec, 0);
> }
>
>
> Thanks!
>
> Jared
> http://jaredrich...
>
>
>