CSC/ECE 517 Fall 2010/ch3 3i IC: Difference between revisions
m (→Introduction) |
|||
(17 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
= Introduction = | = Introduction = | ||
Many middleware solutions have been developed to combine static and dynamic languages to take advantage of both types of code. Previous wiki chapters have discussed JRuby, a Java implementation of Ruby. These solutions allow programmers the productivity luxuries of newer dynamic languages with the speed, low-level functionality, and pre-existing code base of older static languages. Below, we will | Many middleware solutions have been developed to combine static and dynamic languages to take advantage of both types of code. Previous wiki chapters have discussed [http://jruby.org/ JRuby], a Java implementation of Ruby. These solutions allow programmers the productivity luxuries of newer [http://en.wikipedia.org/wiki/Type_checking#Dynamic_typing dynamic languages] (e.g. Ruby, Python, Perl) with the speed, low-level functionality, and pre-existing code base of older [http://en.wikipedia.org/wiki/Type_checking#Static_typing static languages] (e.g. C, C++, Java). | ||
===Advantages of Dynamic Languages=== | |||
* [http://en.wikipedia.org/wiki/Type_checking#Dynamic_typing Dynamic type-checking] | |||
* [http://en.wikipedia.org/wiki/Reflection_%28computer_science%29 Reflection] | |||
* Faster development | |||
===Advantages of Static Languages=== | |||
* Faster execution speed | |||
* Lower memory footprint (especially C/C++) | |||
* Strong potential for larger existing codebase | |||
Below, we will see even more examples of mixing several widely used dynamic languages with C, C++, and Java. | |||
=Mixing Dynamic Code and C/C++= | =Mixing Dynamic Code and C/C++= | ||
Line 9: | Line 24: | ||
==Ruby== | ==Ruby== | ||
===CplusRuby=== | ===CplusRuby Gem=== | ||
CplusRuby is a Ruby gem that allows programmers to call Ruby code within C++ and vice versa. Assuming the user has Ruby installed, the CplusRuby can be installed with the following command [2]: | CplusRuby is a Ruby gem that allows programmers to call Ruby code within C++ and vice versa. Assuming the user has Ruby installed, the CplusRuby can be installed with the following command [http://rubydoc.info/gems/cplus2ruby/1.2.0/frames [2]]: | ||
Line 85: | Line 100: | ||
<pre>import hello | <pre>import hello | ||
print hello.greet() | print hello.greet() | ||
Output: | |||
>> Hello, World!</pre> | >> Hello, World!</pre> | ||
Line 106: | Line 123: | ||
};</pre> | };</pre> | ||
Next is the Boost wrapper code. Like the sample above, we include the Boost header and use the Boost namespace. We then declare the module name by passing it to <code>BOOST_PYTHON_MODULE</code>. Notice the definition of the class and the two functions are defined as one line of code. | |||
<pre> | <pre> | ||
Line 120: | Line 137: | ||
</pre> | </pre> | ||
The Python code below imports the C++ module, creates an instance of the C++ <code>Person</code> class, sets its name to "John" and then outputs the name. | |||
<pre> | <pre> | ||
Line 144: | Line 161: | ||
===Example=== | ===Example=== | ||
== | Below, we declare two simple math functions, <code>add</code> and <code>subtract</code>. | ||
<pre> | |||
/* File : math.c */ | |||
int a = 5; | |||
int b = 9; | |||
int add(int x, int y) | |||
{ | |||
return x + y; | |||
} | |||
int subtract(int x, int y) | |||
{ | |||
return x - y; | |||
} | |||
</pre> | |||
Next we create an interface file, which SWIG will use to register the C functions. | |||
<pre> | |||
/* math.i */ | |||
%module math | |||
%{ | |||
int a | extern int a; | ||
int b | extern int b; | ||
extern int add(int x, int y); | |||
int add(int x, int y) | extern int subtract(int x, int y); | ||
%} | |||
int subtract(int x, int y) | |||
} | |||
extern int a; | |||
extern int b; | |||
extern int add(int x, int y); | |||
extern int subtract(int x, int y); | |||
</pre> | |||
Alternatively, we can include a header file that contains our function definitions. Assuming a math.h file that declares are variables and functions, the code can be wrapped likewise: | |||
<pre> | |||
/* math.i */ | |||
<code | %module math | ||
>>> | %{ | ||
>>> | /* Include header file in wrapper code */ | ||
13 | #include "math.h" | ||
>>> | %} | ||
10 | /* Parse header file to generate wrappers */ | ||
</ | %include "math.h" | ||
</pre> | |||
The following shows the procedure for compiling the C code to use with Python. First, the interface file, math.i, created above is passed to SWIG while specifying the wrapper language as Python. Next, the C | |||
code is compiled while including the Python libraries. Finally, the object files creating by the compilation are linked to create our <code>math_module</code> shared object. (For more information on shared objects, see [http://en.wikipedia.org/wiki/Library_%28computing%29#Shared_libraries [9]]. | |||
(Note: The compilation procedure below is specific to UNIX systems.) | |||
<pre> | |||
$ swig -python math.i | |||
$ gcc -c math.c math_wrap.c -I/usr/include/python | |||
$ ld -shared math.o math_wrap.o -o math_module.so | |||
</pre> | |||
We can now use the C module in Python as follows: | |||
<pre> | |||
>>> import math_module | |||
>>> math_module.add(5,8) | |||
13 | |||
>>> math_module.subtract(15,5) | |||
10 | |||
</pre> | |||
For more examples using other languages besides Python, see the SWIG tutorial. [http://swig.org/tutorial.html [8]] | For more examples using other languages besides Python, see the SWIG tutorial. [http://swig.org/tutorial.html [8]] | ||
Line 204: | Line 242: | ||
===Using Inline::Java=== | ===Using Inline::Java=== | ||
The Inline::Java module allows you to put Java code directly inline with a Perl script. After the Java code has been compiled, Perl queries the Java classes to see what public methods have been defined. These classes and methods are available to the Perl program as if they had been written in Perl. [http://search.cpan.org/~patl/Inline-Java-0.52/Java.pod [5]] | The Inline::Java module allows you to put Java code directly inline with a Perl script. After the Java code has been compiled, Perl queries the Java classes to see what public methods have been defined, since only public methods can be exported to Perl. These classes and methods are available to the Perl program as if they had been written in Perl. [http://search.cpan.org/~patl/Inline-Java-0.52/Java.pod [5]] | ||
The code below shows how to declare and use a Java class in Perl. The below example shows a simple <code>Person</code> class. It has one instance variable and two methods, a getter and setter for that instance variable. | |||
< | <pre>public class Person | ||
{ | { | ||
String name; | |||
public getName() | |||
{ | |||
return name; | |||
} | |||
public setName(String newName) | |||
{ | |||
this.name = newName; | |||
} | |||
}</ | }</pre> | ||
The code below shows how to wrap the Java code in Perl. The actual implementation of the <code>Person</code> class is omitted since it is shown above. | |||
<pre> | |||
<nowiki>#!/usr/bin/perl</nowiki> | <nowiki>#!/usr/bin/perl</nowiki> | ||
use strict; use warnings; | use strict; use warnings; | ||
use Inline Java => <<'EOJ'; | use Inline Java => <<'EOJ'; | ||
public class Person | public class Person | ||
{ | { | ||
// The class body is shown in the Java Code above | |||
} | } | ||
EOJ< | EOJ | ||
</pre> | |||
my $student = Person->new("John Smith"); | The Perl code below shows the instantiation of the <code>Person</code> class. The code also initializes the instance variable <code>name</code> to "John Smith". Finally, the name of the <code>Person</code> object is printed. | ||
<pre> | |||
my $student = Person->new("John Smith"); | |||
print $student->getName(), "\n"; | print $student->getName(), "\n"; | ||
</pre> | |||
(This section on Java and Perl is mostly referenced from [http://www.perl.com/pub/2003/11/07/java.html [4]].) | (This section on Java and Perl is mostly referenced from [http://www.perl.com/pub/2003/11/07/java.html [4]].) | ||
Line 244: | Line 288: | ||
=Conclusion= | =Conclusion= | ||
Several programs and libraries exist to allow programmers to mix static and dynamic code. In the case of mixing modern languages with C and C++, programmers can harness the speed which comes with calling C functions using C++ classes. Regarding Java, programmers can use pre-existing code written in Java, but interface with it with dynamic languages that allow for faster development. In both cases | Several programs and libraries exist to allow programmers to mix static and dynamic code. In the case of mixing modern languages with C and C++, programmers can harness the speed which comes with calling C functions and using C++ classes. Regarding Java, programmers can use pre-existing code written in Java, but interface with it with dynamic languages that allow for faster and easier development. In both cases of mixing dynamic code with C++ and Java, programmers are allowed to work with dynamic languages with less restrictions on typing while using code that is already written, saving time by not having to unnecessarily re-write code. | ||
=References= | =References= | ||
Line 264: | Line 308: | ||
[8] ''SWIG Tutorial''. http://swig.org/tutorial.html | [8] ''SWIG Tutorial''. http://swig.org/tutorial.html | ||
[9] http://en.wikipedia.org/wiki/Library_%28computing%29#Shared_libraries |
Latest revision as of 03:29, 16 October 2010
Mixing Static and Dynamic Code
Introduction
Many middleware solutions have been developed to combine static and dynamic languages to take advantage of both types of code. Previous wiki chapters have discussed JRuby, a Java implementation of Ruby. These solutions allow programmers the productivity luxuries of newer dynamic languages (e.g. Ruby, Python, Perl) with the speed, low-level functionality, and pre-existing code base of older static languages (e.g. C, C++, Java).
Advantages of Dynamic Languages
- Dynamic type-checking
- Reflection
- Faster development
Advantages of Static Languages
- Faster execution speed
- Lower memory footprint (especially C/C++)
- Strong potential for larger existing codebase
Below, we will see even more examples of mixing several widely used dynamic languages with C, C++, and Java.
Mixing Dynamic Code and C/C++
Ruby
CplusRuby Gem
CplusRuby is a Ruby gem that allows programmers to call Ruby code within C++ and vice versa. Assuming the user has Ruby installed, the CplusRuby can be installed with the following command [2]:
gem install cplus2ruby
An example is given below. In this sample, C code is embedded in Ruby code. This uses the cplusruby gem, which is garnered toward C code, while cplus2ruby was designed for C++ code in mind. [2 ] gives an example using CplusRuby in C++ and [6 ] gives the corresponding example in C.
require 'cplusruby' class NeuralEntity < CplusRuby property :id end class Neuron < NeuralEntity property :potential, :float property :last_spike_time, :float property :pre_synapses, :value method_c :stimulate, %(float at, float weight), %{ // this is C code selfc->potential += at*weight; } def initialize self.pre_synapses = [] end end # generate C file, compile it and load the .so CplusRuby.evaluate("inspire.cc", "-O3", "-lstdc++") if __FILE__ == $0 n = Neuron.new n.id = "n1" n.potential = 1.0 n.stimulate(1.0, 2.0) p n.potential # => 3.0 end
In the example above, we declare a class as a C class, but in Ruby syntax. However, we define the method stimulate
in C syntax. We then use CplusRuby to evaluate the embedded C code to generate a C code file. CplusRuby then compiles the generated C code, which then becomes available to the Ruby code declared at the bottom of the code sample. This method allows performance sensitive code to be compiled as C, which is much faster than Ruby. These C methods can also call other C methods with native C performance. [6 ] It's worth pointing out that the properties of the class Neuron
and NeuralEntity
(id, potential, last_spike_time
, etc.) are actually members of a C-struct, not instance variables. Their identity as struct members also allows for faster access.
Python
Using Boost
Boost allows programmers to seamlessly expose C++ classes and function to Python and vice versa. No special tools are needed (however, a build tool will be referenced later). All that is required are the Boost libraries and a C++ compiler. Boost is designed to allow Python to use C++ code with no internal modification to the C++ code that will be wrapped.
Exposing Functions
Below is a “Hello, World!” example. We first define a simple C++ function that returns a "Hello, World" string.
string greet() { return "Hello, World!"; }
We can then add the following wrapper code to our C++ code to expose it Boost. First, we include the Boost header. Then we give the C++ module that we want to use in Python the name of hello
and pass it to the function BOOST_PYTHON_MODULE
, which allows us to declare the module. Within the function, we use the namespace designated for Boost (boost::python
). We also define the name of the function that Python will use to refer to the greet()
C++ function. Naturally, it is the same as the name given in C++ ("greet").
#include <boost/python.hpp> BOOST_PYTHON_MODULE(hello) { using namespace boost::python; def("greet", greet); }
Below is the python code that uses the C++ module. First, we must import the module, which we named hello
above. We can then use the name of the module to call C++ functions.
import hello print hello.greet() Output: >> Hello, World!
Exposing Classes
Below is a simple C++ class with one attribute and a getter and setter for that attribute.
class Person { private string name; void setName(string name) { this->name = name; } string getName() { return name; } };
Next is the Boost wrapper code. Like the sample above, we include the Boost header and use the Boost namespace. We then declare the module name by passing it to BOOST_PYTHON_MODULE
. Notice the definition of the class and the two functions are defined as one line of code.
#include <boost/python.hpp> using namespace boost::python; BOOST_PYTHON_MODULE(person_module) { class_<Person>("Person") .def("getName", &Person::getName) .def("setName", &Person::setName); }
The Python code below imports the C++ module, creates an instance of the C++ Person
class, sets its name to "John" and then outputs the name.
import person_module student = person_module.Person() student.setName('John') student.getName() Output: >> John
SWIG
Previously we've seen specific examples of middleware libraries that allow us to mix specific languages with C/C++. SWIG (Simplified Wrapper and Interface Generator) is a tool that allows programmers to mix C/C++ programs and libraries with several dynamic languages, including Tcl, Perl, Python, Ruby, PHP, and Lua. SWIG was first released in 1995 and is still maintained to this day, with its most recent release on October 4, 2010.
SWIG maintains two purposes for embedding dynamic language code in C/C++
- Faster customization using the scripting language
- Easier to write test scripts, including unit tests
SWIG allows programmers to replace the main() function of a C program with a scripting interpreter from which they can control the application. This ability adds flexibility and makes the program more easily modifiable. The scripting interface allows programmers to easily modify program behavior without having to modify low-level C/C++ code.
Example
Below, we declare two simple math functions, add
and subtract
.
/* File : math.c */ int a = 5; int b = 9; int add(int x, int y) { return x + y; } int subtract(int x, int y) { return x - y; }
Next we create an interface file, which SWIG will use to register the C functions.
/* math.i */ %module math %{ extern int a; extern int b; extern int add(int x, int y); extern int subtract(int x, int y); %} extern int a; extern int b; extern int add(int x, int y); extern int subtract(int x, int y);
Alternatively, we can include a header file that contains our function definitions. Assuming a math.h file that declares are variables and functions, the code can be wrapped likewise:
/* math.i */ %module math %{ /* Include header file in wrapper code */ #include "math.h" %} /* Parse header file to generate wrappers */ %include "math.h"
The following shows the procedure for compiling the C code to use with Python. First, the interface file, math.i, created above is passed to SWIG while specifying the wrapper language as Python. Next, the C
code is compiled while including the Python libraries. Finally, the object files creating by the compilation are linked to create our math_module
shared object. (For more information on shared objects, see [9].
(Note: The compilation procedure below is specific to UNIX systems.)
$ swig -python math.i $ gcc -c math.c math_wrap.c -I/usr/include/python $ ld -shared math.o math_wrap.o -o math_module.so
We can now use the C module in Python as follows:
>>> import math_module >>> math_module.add(5,8) 13 >>> math_module.subtract(15,5) 10
For more examples using other languages besides Python, see the SWIG tutorial. [8]
Mixing Dynamic Code and Java
Perl
Using Inline::Java
The Inline::Java module allows you to put Java code directly inline with a Perl script. After the Java code has been compiled, Perl queries the Java classes to see what public methods have been defined, since only public methods can be exported to Perl. These classes and methods are available to the Perl program as if they had been written in Perl. [5]
The code below shows how to declare and use a Java class in Perl. The below example shows a simple Person
class. It has one instance variable and two methods, a getter and setter for that instance variable.
public class Person { String name; public getName() { return name; } public setName(String newName) { this.name = newName; } }
The code below shows how to wrap the Java code in Perl. The actual implementation of the Person
class is omitted since it is shown above.
#!/usr/bin/perl use strict; use warnings; use Inline Java => <<'EOJ'; public class Person { // The class body is shown in the Java Code above } EOJ
The Perl code below shows the instantiation of the Person
class. The code also initializes the instance variable name
to "John Smith". Finally, the name of the Person
object is printed.
my $student = Person->new("John Smith"); print $student->getName(), "\n";
(This section on Java and Perl is mostly referenced from [4].)
In the Perl example above, we omit the details of the Java class, since they are detailed above. The declaration 'EOJ' ("end of Java", in this case) at the beginning of the Java class declaration in the Perl code is a qualifier to dictate where to end the Java code. In this particular example, we include the Java code directly in our Perl file. However, you can also include Java code via a file reference.
Conclusion
Several programs and libraries exist to allow programmers to mix static and dynamic code. In the case of mixing modern languages with C and C++, programmers can harness the speed which comes with calling C functions and using C++ classes. Regarding Java, programmers can use pre-existing code written in Java, but interface with it with dynamic languages that allow for faster and easier development. In both cases of mixing dynamic code with C++ and Java, programmers are allowed to work with dynamic languages with less restrictions on typing while using code that is already written, saving time by not having to unnecessarily re-write code.
References
[1] http://en.wikipedia.org/wiki/SWIG
[2] Michael Neumann. Cplus2Ruby - Gluing C++ and Ruby together in an OO manner. http://rubydoc.info/gems/cplus2ruby/1.2.0/frames
[3] Dave Abrahams. Boost.Python. http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html
[4] Phil Crow. Bringing Java into Perl. http://www.perl.com/pub/2003/11/07/java.html. 2003.
[5] Patrick LeBoutillier. Inline::Java http://search.cpan.org/~patl/Inline-Java-0.52/Java.pod
[6] Michael Neumann. CplusRuby - Gluing C and Ruby. http://www.ntecs.de/blog/articles/2007/09/21/cplusruby-gluing-c-and-ruby/. 2007.
[7] What is SWIG? http://www.swig.org/exec.html
[8] SWIG Tutorial. http://swig.org/tutorial.html
[9] http://en.wikipedia.org/wiki/Library_%28computing%29#Shared_libraries