Search code examples
rubyswiglibqxt

Ruby with Swig: NameError: uninitialized constant


I'm trying to use Qxt library (namely QxtGlobalShortcut) in the ruby.

As suggested on: How can I call C++ functions from within ruby I created swig wrapper, however when trying to use generated library I'm stuck with error:

NameError: uninitialized constant QxtGlobalShortcut::QxtGlobalShortcut
    from (irb):5
    from /usr/bin/irb:12:in `<main>'

My full irb session output:

$ irb
irb(main):001:0> require 'Qt4'
=> true
irb(main):002:0> require 'QxtGlobalShortcut'
=> true
irb(main):003:0> app = Qt::Application.new ARGV
=> #<Qt::Application:0x0000000110bd18 objectName="irb">
irb(main):004:0> ui = Qt::Widget.new
=> #<Qt::Widget:0x000000012a8428 objectName="", x=0, y=0, width=640, height=480>
irb(main):005:0> shortcut = QxtGlobalShortcut::QxtGlobalShortcut.new ui
NameError: uninitialized constant QxtGlobalShortcut::QxtGlobalShortcut
    from (irb):5
    from /usr/bin/irb:12:in `<main>'

I used following to generate the wrapper in swig:

$ cat QxtGlobalShortcut.i
%module QxtGlobalShortcut
%{
/* Includes the header in the wrapper code */
#include "/usr/include/QxtGui/QxtGlobalShortcut"
%}

/* Parse the header file to generate wrappers */
%include "/usr/include/QxtGui/QxtGlobalShortcut"

$ cat extconf.sh
require 'mkmf'
dir_config('QxtCore')
dir_config('QxtGui')
dir_config('QtCore')
dir_config('QtGui')
create_makefile('QxtGlobalShortcut')

$ cat wrapper.sh
swig -c++ -ruby QxtGlobalShortcut.i
ruby extconf.rb --with-QxtCore-include=/usr/include/QxtCore/ --with-QxtGui-include=/usr/include/QxtGui/ --with-QtCore-include=/usr/include/QtCore/ --with-QtGui-include=/usr/include/QtGui/
make 
sudo make install

Any idea how to fix it?


Solution

  • According to the generated .cpp file in the provided gist, there are no wrapped types generated, see line 1814 to 1822:

    /* -------- TYPES TABLE (BEGIN) -------- */
    
    #define SWIGTYPE_p_char swig_types[0]
    static swig_type_info *swig_types[2];
    static swig_module_info swig_module = {swig_types, 1, 0, 0, 0, 0};
    #define SWIG_TypeQuery(name) SWIG_TypeQueryModule(&swig_module, &swig_module, name)
    #define SWIG_MangledTypeQuery(name) SWIG_MangledTypeQueryModule(&swig_module, &swig_module, name)
    
    /* -------- TYPES TABLE (END) -------- */
    

    Well, in fact there's only one which is p_char. For an unknown reason from here, your %include "/usr/include/QxtGui/QxtGlobalShortcut" clause in the .i file doesn't generate any wrapping.

    Maybe the content of this header is "protected" by #ifdef's that are evaluated as FALSE and thus SWIG ignore its content. You really have to check this header, or post a link here to a gist of its content, so that we may edit this answer.