Question

I am following the tutorial for ruby Rice at http://rice.rubyforge.org/index.html. My end goal is to be able to wrap a c++ object that I already have running, but call it from Ruby.

In following the tutorial I can create a C++ class fine and call methods that I define, however, once I get to the wrapping of a pre-existing C++ object I get a symbol lookup error:

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);
}

I have very little experience in ruby, none in Rice. Am I doing something wrong? It seems I don't know enough about shared libraries to debug thoroughly. If it helps, I get this when running 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)

but I don't understand why it is undefined when the class came from the same file.

What am I missing and why is this happening?

Was it helpful?

Solution

Your constructor seems to be just declared not defined :

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

Just try :

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

In your error, it is searching for the definition of the constructor without success.

_ZN4TestC1Ev is the name of your constructor decorated during the compilation. Search for name mangling.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top