From: Clemens Hintze Date: 2000-08-22T15:40:02+09:00 Subject: [ruby-talk:4539] Re: a question about extension modules --Multipart_Mon_Aug_21_22:38:34_2000-1 Content-Type: text/plain; charset=US-ASCII >>>>> "Jon" == Jon Aseltine 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_ 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 --Multipart_Mon_Aug_21_22:38:34_2000-1 Content-Type: application/octet-stream; type=tar+gzip Content-Disposition: attachment; filename="wrapping-c++.tar.gz" Content-Transfer-Encoding: base64 H4sIAJaSoTkAA+1YbW/bNhD2Z/2KgzKgkmO78juQFw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