CSC/ECE 517 Spring 2014/ch1 1w1h jg: Difference between revisions
No edit summary |
No edit summary |
||
Line 11: | Line 11: | ||
=== interpreter API === | === interpreter API === | ||
Ruby interpreter is implemented in C, its API can be used and no special API added for interacting with C like Java’s JNI is needed. | Ruby interpreter is implemented in C, its API can be used and no special API added for interacting with C like Java’s JNI is needed. | ||
*'''mkmf (make makefile) Ruby Module''' | |||
For this usage, platform-specific Makefiles for compiling C extensions to Ruby is needed to be generated firstly. | For this usage, platform-specific Makefiles for compiling C extensions to Ruby is needed to be generated firstly. | ||
A simple sample is like below | A simple sample is like below: | ||
create a file containing the following, | |||
named extconf.rb by convention | |||
<pre> | <pre> | ||
require 'mkmf' | require 'mkmf' | ||
Line 20: | Line 22: | ||
create_makefile(extension_name) | create_makefile(extension_name) | ||
</pre> | </pre> | ||
And use by running | |||
<pre> | <pre> | ||
ruby extconf.rb | ruby extconf.rb | ||
make | make | ||
</pre> | </pre> | ||
Then generates | |||
<pre> | <pre> | ||
.so under UNIX/Linux | .so under UNIX/Linux |
Revision as of 21:21, 4 February 2014
Ruby libraries to load objects of other languages at run time
Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It is easy to extend Ruby with new features by writing code in Ruby. But every now and then extending ruby with low-level languages, such asC/C++/Java is also necessary.
Currently, various kinds of languages codes could be invoked from within ruby. The extension for C/C++ and Java are focused here.
Ruby C/C++ extensions<ref>http://java.ociweb.com/mark/NFJS/RubyCExtensions.pdf</ref>
By extending Ruby with C. The C libraries could be used directly in Ruby applications. Ruby could call C codes in three ways: interpreter API, RubyInline, SWIG.
interpreter API
Ruby interpreter is implemented in C, its API can be used and no special API added for interacting with C like Java’s JNI is needed.
- mkmf (make makefile) Ruby Module
For this usage, platform-specific Makefiles for compiling C extensions to Ruby is needed to be generated firstly. A simple sample is like below: create a file containing the following, named extconf.rb by convention
require 'mkmf' extension_name = 'name' dir_config(extension_name) create_makefile(extension_name)
And use by running
ruby extconf.rb make
Then generates
.so under UNIX/Linux .so under Windows when building with Cygwin .bundle under Mac OS X