swig problem with init

J

jim

I have an innocuous swig/ruby case that is causing me some grief.
The problem is probably obvious, but I'm not seeing the solution
right now.

I have a .i file that defines a module

cat itkdb.i
/* itkdb.i */
%module ITKDb

%{
%}

This generates a wrapper with the following lines of interest:

#define SWIG_init Init_ITKDb
#define SWIG_name "ITKDb"

#ifdef __cplusplus
extern "C"
#endif
SWIGEXPORT(void) Init_ITKDb(void) {
int i;

SWIG_InitRuntime();
mITKDb = rb_define_module("ITKDb");

for (i = 0; swig_types_initial; i++) {
swig_types = SWIG_TypeRegister(swig_types_initial);
SWIG_define_class(swig_types);
}

}

I compile this against a vendor supplied library and try to load the .so
file and get:

ruby -r itkdb -e {}
./itkdb.so: /usr/lib/libruby.so.1.8: undefined symbol: Init_itkdb -
./itkdb.so (LoadError)

Can someone tell me what I'm doing wrong here?
 
J

Joel VanderWerf

I have an innocuous swig/ruby case that is causing me some grief.
The problem is probably obvious, but I'm not seeing the solution
right now.

I have a .i file that defines a module

cat itkdb.i
/* itkdb.i */
%module ITKDb

%{
%}

This generates a wrapper with the following lines of interest:

#define SWIG_init Init_ITKDb
#define SWIG_name "ITKDb"

#ifdef __cplusplus
extern "C"
#endif
SWIGEXPORT(void) Init_ITKDb(void) {
int i;

SWIG_InitRuntime();
mITKDb = rb_define_module("ITKDb");

for (i = 0; swig_types_initial; i++) {
swig_types = SWIG_TypeRegister(swig_types_initial);
SWIG_define_class(swig_types);
}

}

I compile this against a vendor supplied library and try to load the .so
file and get:

ruby -r itkdb -e {}
./itkdb.so: /usr/lib/libruby.so.1.8: undefined symbol: Init_itkdb -
./itkdb.so (LoadError)

Can someone tell me what I'm doing wrong here?


Ruby's loader looks for init_<filename> in filename.so. So if you

mv itkdb.so ITKDb.so

it may work.
 
P

Phil Tomson

I have an innocuous swig/ruby case that is causing me some grief.
The problem is probably obvious, but I'm not seeing the solution
right now.

I have a .i file that defines a module

cat itkdb.i
/* itkdb.i */
%module ITKDb

%{
%}

This generates a wrapper with the following lines of interest:

#define SWIG_init Init_ITKDb
#define SWIG_name "ITKDb"

#ifdef __cplusplus
extern "C"
#endif
SWIGEXPORT(void) Init_ITKDb(void) {
int i;

SWIG_InitRuntime();
mITKDb = rb_define_module("ITKDb");

for (i = 0; swig_types_initial; i++) {
swig_types = SWIG_TypeRegister(swig_types_initial);
SWIG_define_class(swig_types);
}

}

I compile this against a vendor supplied library and try to load the .so
file and get:

ruby -r itkdb -e {}
./itkdb.so: /usr/lib/libruby.so.1.8: undefined symbol: Init_itkdb -
./itkdb.so (LoadError)

Can someone tell me what I'm doing wrong here?


Hmmmmm...
Some quick guesses:

In your wrapper file you've got this declaration:
SWIGEXPORT(void) Init_ITKDb(void) {

While the error says it's looking for the symbol:
Init_itkdb
-------^^^^

So there seems to be a mismatch between ITKDb and itkdb...

I would try changing the .i file to:

%module itkdb

%{
%}


Although it does seem a bit backward... If I recall correctly, what
happens is that when you define '%module itkdb' you'll get a Ruby module
called Itkdb (uppercased). In your case you called it ITKDb and it seems
to have lowercased it.


What happens when you do:
nm itkdb.so | grep Init

? Does it have entries for both Init_ITKDb (defined) and
Init_itkdb (undefined)?

Also, what version of swig are you running?

Phil
 
L

Lyle Johnson

I would try changing the .i file to:

%module itkdb

%{
%}

Although it does seem a bit backward... If I recall correctly, what
happens is that when you define '%module itkdb' you'll get a Ruby module
called Itkdb (uppercased).

Yes, that is exactly what happens.
In your case you called it ITKDb and it seems
to have lowercased it.

No, look at the SWIG-generated wrapper code, e.g.

mITKDb = rb_define_module("ITKDb");

SWIG definitely honored his module name choice of ITKDb. What does
seem to have happened is that Jim then compiled the wrapper code into
a shared library named (lowercase) itkdb.so, which (as Joel as pointed
out) is inconsistently named. SWIG just generates the source code and
doesn't have any control over how you actually compile that code into
a shared library for use by Ruby.
 
J

jim

* Lyle Johnson said:
No, look at the SWIG-generated wrapper code, e.g.

mITKDb = rb_define_module("ITKDb");

SWIG definitely honored his module name choice of ITKDb. What does
seem to have happened is that Jim then compiled the wrapper code into
a shared library named (lowercase) itkdb.so, which (as Joel as pointed
out) is inconsistently named. SWIG just generates the source code and
doesn't have any control over how you actually compile that code into
a shared library for use by Ruby.

Yes, that is exactly what happened, and I recognized it immediately after
Joel's and Phil's email. I have run across this before, but
had forgotten about it.

Sorry for the noise, but maybe it's good for the archives.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top