[#4567] Re: What's the biggest Ruby development? — Aleksi Niemel<aleksi.niemela@...>

Dave said:

18 messages 2000/08/23
[#4568] Q's on Marshal — Robert Feldt <feldt@...> 2000/08/23

[#4580] RubyUnit testcase run for different init params? — Robert Feldt <feldt@...> 2000/08/25

[#4584] Re: RubyUnit testcase run for different init params? — Dave Thomas <Dave@...> 2000/08/25

Robert Feldt <feldt@ce.chalmers.se> writes:

[#4623] Re: RubyUnit testcase run for different init params? — Robert Feldt <feldt@...> 2000/08/28

On Sat, 26 Aug 2000, Dave Thomas wrote:

[#4652] Andy and Dave's European Tour 2000 — Dave Thomas <Dave@...>

24 messages 2000/08/30
[#4653] Re: Andy and Dave's European Tour 2000 — matz@... (Yukihiro Matsumoto) 2000/08/30

Hi,

[#4657] Ruby tutorials for newbie — Kevin Liang <kevin@...> 2000/08/30

Hi,

[ruby-talk:4539] Re: a question about extension modules

From: Clemens Hintze <c.hintze@...>
Date: 2000-08-22 06:40:02 UTC
List: ruby-talk #4539
>>>>> "Jon" == Jon Aseltine <aseltine@cs.umass.edu> writes:

    Jon> I asked once, but perhaps I wasn't clear enough. Is it
    Jon> possible to implement an extension module for a C++ library
                                                   ^^^^^^^^^^^^^^^^^

And again, you are not clear enough (sorry to say!) ;-)

    Jon> (for example, TrollTech's QT library)? The README.EXT file
    Jon> focuses on C libraries, not C++.

Reading this I assume you want to wrap a C++ library to be used as an
extension in Ruby, isn't it?

If yes, you could follow Yasushi Shoji's advice to try out
swigruby. To use this you have to install SWIG as well. It will wrap
your library for you. But be warned: SWIG has some ugly restrictions
that may not permit you to wrap your code (I am not sure for QT,
however). Furthermore the code that is generated is not optimal all
the time. But OTOH, it is easy to use and its result is often very
useable.

If you want to do it manually, you have to pay attention to certain
points. Roughly spoken, you have to wrap the C++ library to be useable 
under Plain C! Then you can also wrap it for Ruby! You couldn't use
classes on the C interface side! And you have to look for name
mangeling that a C++ compiler do. For example the Init_<extension>
function has to use C linkage and naming instead of C++ one!

Then you have to write functions for every method you want to call!
This because C cannot call the method of an instance. Last but not
least you would have to compile you extension with a C++ compiler and
do not forget to link-in the C++ runtime library (e.g. libstdc++).

I have attached a small example to this message. Here two files will
define the C++ class that we want to wrap for Ruby. 

  point_t.h
  point_t.cpp

Both files defines a C++ class point_t with methods. We want to make
instances of this class available as class Point in Ruby. For the
methods 

  point_t::point_t  // constructor
  point_t::move
  point_t::show

we define functions that call the methods on the corresponding
instance. The instance itself will be wrapped with
Data_Wrap_Struct. Consider the macro RUBYFN that casts a function
pointer to the one Ruby need to store it into its method table. The
wrapping will be done in file

  point.cpp

To compile all these simply first run 

  ruby extconf.rb
  make

Then try

  ruby test.rb


    Jon> TYIA,

    Jon> Jon --

HTH,
\cle


Attachments (1)

wrapping-c++.tar.gz (1.02 KB, Archive)

In This Thread