【问题标题】:Unable to complete simple example for Rice (Ruby). What am I missing?无法完成 Rice (Ruby) 的简单示例。我错过了什么?
【发布时间】:2013-08-21 05:35:36
【问题描述】:

我正在关注http://rice.rubyforge.org/index.html 的 ruby​​ Rice 教程。我的最终目标是能够包装一个我已经在运行的 c++ 对象,但可以从 Ruby 中调用它。

按照本教程,我可以创建一个 C++ 类并调用我定义的方法,但是,一旦我包装了一个预先存在的 C++ 对象,我就会得到一个符号查找错误:

cam@Pele:~/localProjects/rubyTest$ ruby extconf.rb
creating Makefile
cam@Pele:~/localProjects/rubyTest$ make
linking shared-object Test.so
cam@Pele:~/localProjects/rubyTest$ irb
irb(main):001:0> require './Test'
=> true
irb(main):002:0> test=Test.new
irb: symbol lookup error: /home/cam/localProjects/rubyTest/Test.so: undefined symbol: _ZN4TestC1Ev

extconf.rb:

require 'mkmf-rice'
create_makefile('Test')

test.cpp

#include "rice/Data_Type.hpp"
#include "rice/Constructor.hpp"

using namespace Rice;

class Test
{
public:
    Test();
    std::string sayHello()
    {
        return std::string("hello");
    }
};

extern "C"
void Init_Test()
{
   Data_Type<Test> rb_cTest = 
    define_class<Test>("Test")
    .define_constructor(Constructor<Test>())
    .define_method("hello", &Test::sayHello);
}

我在 ruby​​ 方面的经验很少,在 Rice 方面没有。难道我做错了什么?看来我对共享库的了解还不够,无法彻底调试。如果有帮助,我会在运行 ldd -d -r Test.so

时得到这个
    linux-vdso.so.1 =>  (0x00007fff5efff000)
    libruby-1.9.1.so.1.9 => /usr/lib/libruby-1.9.1.so.1.9 (0x00007fd24539b000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd24517e000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fd244e7d000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd244abe000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fd2448a8000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fd24469f000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd24449b000)
    libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007fd244262000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fd243f65000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fd2459fb000)
undefined symbol: _ZN4TestC1Ev  (./Test.so)

但我不明白为什么当类来自同一个文件时它是未定义的。

我错过了什么,为什么会这样?

【问题讨论】:

    标签: c++ ruby ruby-c-extension


    【解决方案1】:

    你的构造函数似乎只是 declared 而不是 defined :

    class Test
    {
    public:
        Test();   // <- Just declared.
     // ...
    };
    

    试试看:

    class Test
    {
    public:
        Test() {} // <- Now defined
     // ...
    };
    

    在你的错误中,它正在搜索构造函数的定义但没有成功。

    _ZN4TestC1Ev 是编译期间构造函数的名称​​decorated。搜索name mangling。

    【讨论】:

      猜你喜欢
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多