【问题标题】:Ruby with Swig: NameError: uninitialized constantRuby 与 Swig:NameError:未初始化的常量
【发布时间】:2013-03-03 10:08:05
【问题描述】:

我正在尝试在 ruby​​ 中使用Qxt library(即QxtGlobalShortcut)。

按照How can I call C++ functions from within ruby 的建议,我创建了swig 包装器,但是在尝试使用生成的库时,我遇到了错误:

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

我的完整 irb 会话输出:

$ 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>'

我使用以下方法在 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

知道怎么解决吗?

【问题讨论】:

  • 可能命名空间处理不正确,要检查这一点,找到生成的 QxtGlobalShortcut.cpp 文件,然后发布 Init_QxtGlobalShortcut() 函数的前 20-30 行(在文件末尾),或检查其内容以确定您的 QxtGlobalShortcut 类是否正确放入您的 QxtGlobalShortcut 命名空间。
  • 感谢您的想法,请参阅粘贴在此处的您生成的文件 (QxtGlobalShortcut_wrap.cxx):gist.github.com/typekpb/5076296 我想我仍然需要帮助来弄清楚我的用法出了什么问题

标签: ruby swig libqxt


【解决方案1】:

根据提供的 gist 中生成的 .cpp 文件,没有生成包装类型,见第 1814 到 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) -------- */

嗯,实际上只有一个是 p_char。由于未知原因,.i 文件中的 %include "/usr/include/QxtGui/QxtGlobalShortcut" 子句不会生成任何换行。

也许此标头的内容受到 #ifdef 的“保护”,这些内容被评估为 FALSE,因此 SWIG 会忽略其内容。您确实必须检查此标题,或在此处发布其内容要点的链接,以便我们可以编辑此答案。

【讨论】:

  • 感谢支持,您是对的,引用文件的内容只是:#include "qxtglobalshortcut.h"。但是,在修复正确头文件的路径时,我更进一步:stackoverflow.com/questions/15197131/…。但这似乎是一个不同的问题,因此将您的答案标记为正确的答案(但也许您仍然可以帮助我解决下一个问题)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
相关资源
最近更新 更多