Wrapping C++ with Perl Brian Magill March 2009. SWIG (Simplified Wrapper and Interface Generator) Wraps C++ with Perl or other computer languages Generates.

Slides:



Advertisements
Similar presentations
Introduction to C++ Templates Speaker: Bill Chapman, C++ developer, financial company in Mid-Manhattan. Can be reached via: ' This.
Advertisements

C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.
Binding python to other languages (Fortran and C).
1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Guide To UNIX Using Linux Third Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Guide To UNIX Using Linux Third Edition
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
 2006 Pearson Education, Inc. All rights reserved Generics.
Standard library types Practical session # 3 Software Engineering
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Programming With C.
Writing Perl Extensions: There’s more than one way to do it.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
Defining New Types Lecture 21 Hartmut Kaiser
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Beginning C++ Through Game Programming, Second Edition
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Python/Tango Client Binding Swig ( Vs Boost (
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
Navigation and Ancillary Information Facility NIF IDL Interface to CSPICE “Icy” How to Access the CSPICE library Using Interactive Data Language (IDL)
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI Programming & The CGI.pm Perl Module Instructor: Joseph DiVerdi,
Computing and Statistical Data Analysis Lecture 7 STL and the Standard C++ Library vector, string,... Inheritance (quick tour) Some tools: compiling/linking.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Department of Electronic & Electrical Engineering Introduction to C - The Development cycle. Why C? The development cycle. Using Visual Studio ? A simple.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
C is a high level language (HLL)
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Perl Subroutines User Input Perl on linux Forks and Pipes.
Updates to CaPython/CaChannel John Hammonds Beamline Controls and Data Acquisition Group APS Engineering Support Division EPICS Collaboration Meeting INFN.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Pyragen A PYTHON WRAPPER GENERATOR TO APPLICATION CORE LIBRARIES Fernando PEREIRA, Christian THEIS - HSE/RP EDMS tech note:
Scuola Superiore Sant’Anna Advanced Course on C++ IV Giuseppe Lipari.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
UMBC CMSC 104 – Section 01, Fall 2016
Topic Pre-processor cout To output a message.
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Chapter 1 c++ structure C++ Input / Output
An Introduction to STL.
SPL – PS1 Introduction to C++.
Presentation transcript:

Wrapping C++ with Perl Brian Magill March 2009

SWIG (Simplified Wrapper and Interface Generator) Wraps C++ with Perl or other computer languages Generates the connecting wrapper code for both C++ and Perl (or other target language). C++ code then compiled and linked into a library Perl scripts call Perl wrapper, which in turn calls C++ library

Motivation Code high level functionality in Perl: – File, Database I/O – String manipulation, pattern matching, etc. – Sorting, organizing data – Applying functions to elements of arrays – Filtering out elements from arrays Code Toolkit interfaces and computationally intensive code in C++ Need a better way to integrate these two languages

File Generation example.i example.h (other header files) example.pm example_wrap.cxx SWIG example.cpp (other CPP files) example.dylib (MacIntosh) g++ example.so (Other Unix Platforms) or example_wrap.cxx

SWIG Interface File Tells which routines are to be wrapped Wraps built-in C/C++ data types Wraps STL vectors and strings Can wrap more complex data structures Can specify error handling

Example Interface File %include "exception.i" %exception { try { $action } catch (const std::exception &e) { SWIG_exception_fail(SWIG_RuntimeError, e.what()); } %module example %{ /* Put headers and other declarations here */ #include "example.h" %} %include "std_vector.i" %include "std_string.i" %template(IntVector) std::vector ; %template(DoubleVector) std::vector ; %include "example.h"

Running the SWIG Command  swig –c++ -perl5 example.i Produces: example_wrap.cxx … C++ Wrapper example.pm … Perl Wrapper

Compiling and Linking C++ Example # # Make file: MacIntosh version # all: swig -c++ -perl5 example.i g++ -c example.cpp example_wrap.cxx \ `/usr/bin/perl -MExtUtils::Embed -e ccopts` g++ -dynamiclib -single_module -flat_namespace \ -undefined suppress -o example.dylib example.o example_wrap.o clean: rm *.o *.cxx purge: rm *.o *.dylib *.cxx *.pm

Compiling and Linking C++ Example # # SWIG example make file for Linux # all: swig -c++ -perl example.i g++ -c example.cpp example_wrap.cxx \ `/usr/bin/perl -MExtUtils::Embed -e ccopts` g++ -shared example.o example_wrap.o -L /usr/lib/ \ -lperl -o example.so clean: rm *.o *.cxx purge: rm *.o *.so *.cxx *.pm

Resulting C++ Library Differs with platform and operating system On Mac OS X a *.dylib library is created On Unix Systems a shared (*.so) library is created C++ library needs to be in same location as Perl wrapper.

Simple Example /* Example.h */ #include #define PGSd_EOS_AM 2222 #define PGSd_EOS_PM 3333 double average(std::vector v); std::vector half(const std::vector & v); std::string TestMod(const std::string &s);

Simple Example (Con’t) // // example.cpp // #include #include "example.h" using namespace std; double average(vector v) { return (accumulate(v.begin(), v.end(), 0.0))/v.size();} vector half(const vector & v) { vector w(v); for( unsigned long i = 0; i < w.size(); i++) w[i] /= 2.0; return w; } std::string TestMod(const std::string &s) {return string("TestOutput: ") + s;}

#!/usr/bin/perl # # TestExample.pl # use strict; use example; = (0..3); = (0.0, 1.5, 1.0, 1.5); print "Integer Array Values: "; map { print "$_,"; print "\nAverage = ", "\n"; print "\nTry passing in an array of doubles: \n"; eval {"Average = ", "\n"; }; print "\tError encountered: if # print "Double Array Values: "; map { print "$_,"; my $ref = print "\nHalved Values: "; map { print "$_,"; print "\n\n"; # my $str = "Hello World!\n"; print example::TestMod($str); print "\nConstant PGSd_EOS_AM = ", $example::PGSd_EOS_AM, "\n"; print "Constant PGSd_EOS_PM = ", $example::PGSd_EOS_PM, "\n\n";

TestExample.pl’s Output Integer Array Values: 0,1,2,3, Average = 1.5 Try passing in an array of doubles: Error encountered: RuntimeError Type error in argument 1 of average. Expected an array of int Double Array Values: 0,1.5,1,1.5, Halved Values: 0,0.75,0.5,0.75, TestOutput: Hello World! Constant PGSd_EOS_AM = 2222 Constant PGSd_EOS_PM = 3333

Suggestions Use built-in or STL types arguments wherever possible Otherwise use custom built C++ data classes consisting of above types Link C++ code as shared objects if possible Place exception handling first in interface file Use SWIG_exception_fail

References /CERES/instrument/home/bmagill/ComputerLanguages/Perl/SWIG/SWIG_C++/ /CERES/instrument/home/bmagill/SolarAngle/Configured/src/