I’m trying to use a C++ shared library from Ruby and wanted to use
Ruby/DL to do it. It seemed straightforward enough, but I can’t seem
to match the signature on the function I want to import.
extern “C” void place( const std::string &text)
{
std::cout << text << std::endl ;
}
:
It works with C functions (change the “const std::string &” to a
“const char *” and it’s all good).
I have a feeling this can work with the write typealias and encoding
functions, but I’m fairly new to Ruby and would appreciate any
pointers.
A const std::string & is not a const char *.
C (dl.so) doesn’t know how to construct/destruct std::string.
Use C-compatible types only, or write an extension library using
Ruby APIs. SWIG is one of nice solution I think.
To write a generic C++/Ruby interface is as hard as to write a
C++ compiler.
You will run into a number of problems using Ruby/DL with C++ code:
C++ function names are mangled, but there is no standard for how
this mangling is done. You will need to mangle your function name
in the same way as the C++ implementation on your platform does.
C++ exceptions are not compatible with Ruby exceptions. If your C++
code throws an exception, then your program will have undefined
behavior (it will likely call abort()).
C++ objects have constructors and destructors, and Ruby/DL doesn’t
know how to deal with these. So if your C++ code returns an object
by value, you might be able to get Ruby to wrap it, but getting Ruby
to destruct the object when it is no longer used is a different
story.
C++ has references in addition to pointers. Ruby/DL doesn’t know
how to deal with references, though I suspect they should work
similar to pointers.
You are probably better off using swig instead.
Paul
···
On Fri, Mar 26, 2004 at 12:04:28PM +0900, Bheeshmar Redheendran wrote:
I’m trying to use a C++ shared library from Ruby and wanted to use
Ruby/DL to do it. It seemed straightforward enough, but I can’t seem
to match the signature on the function I want to import.
extern “C” void place( const std::string &text)
{
std::cout << text << std::endl ;
}
:
It works with C functions (change the “const std::string &” to a
“const char *” and it’s all good).
I have a feeling this can work with the write typealias and encoding
functions, but I’m fairly new to Ruby and would appreciate any
pointers.
A const std::string & is not a const char *.
C (dl.so) doesn’t know how to construct/destruct std::string.
Use C-compatible types only, or write an extension library using
Ruby APIs. SWIG is one of nice solution I think.
Swig is great when you’ve got the source code for the library available.
If you’ve got a shared library and an API reference for it (but no source
code) then Ruby/DL seems like the way to go.
Are you saying that Ruby/DL won’t work with a shared library that was
developed in C++?