Swig problem with init

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]; i++) {
            swig_types[i] = SWIG_TypeRegister(swig_types_initial[i]);
            SWIG_define_class(swig_types[i]);
        }

    }

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?

···

--
Jim Freeze

jim@freeze.org wrote:

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]; i++) {
            swig_types[i] = SWIG_TypeRegister(swig_types_initial[i]);
            SWIG_define_class(swig_types[i]);
        }

    }

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.

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]; i++) {
           swig_types[i] = SWIG_TypeRegister(swig_types_initial[i]);
           SWIG_define_class(swig_types[i]);
       }

   }

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

···

In article <20040818221509.GA70952@freeze.org>, <jim@freeze.org> wrote:

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.

···

On Thu, 19 Aug 2004 08:35:54 +0900, Phil Tomson <ptkwt@aracnet.com> wrote:

* Lyle Johnson <lyle.johnson@gmail.com> [2004-08-19 22:18:07 +0900]:

···

On Thu, 19 Aug 2004 08:35:54 +0900, Phil Tomson <ptkwt@aracnet.com> wrote:

> 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.

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.
--
Jim Freeze