How can I use a C++ class from Perl? - c++

I have a set of classes written in C++. What would be best way to call them from a Perl script? Thanks.

I'm not particularly fond of SWIG and prefer to write the interfacing code myself. Perl comes with a sort of pseudo language called 'XS' for interfacing to C or C++. Unfortunately, in order to use it, you will need to know at least C, Perl, and then learn something about the interpreter API, too. If you already know Perl and C well, it's not such a big step. Have a look at the following core documents on XS:
perlxstut (XS tutorial)
perlxs (XS reference)
perlapi (Interpreter API)
Additionally, there's plenty of tutorials and how-tos on the internet.
Now, interfacing to C++ using XS requires some additional steps. It can be a bit frustrating to work out at first, but neatly falls into place once you get it. In this regard, the core documentation is sparse at best. But all is not lost. Mattia Barbon, the creator of the wxWidgets bindings for Perl, wrote a great tool "XS++" that makes this almost dead simple (or as simple as XS). It's included in Wx, but we're working on splitting it out into its own distribution. This is work in progress. You can find Mattia's XS++ code and a modified version of mine on github.
Barring a release of a standalone XS++ to CPAN, I would suggest learning to write XS for C++ from other resources:
Quite a long time ago, John Keiser wrote an excellent tutorial on XS and C++. It also includes further pointers to useful tools and documentation.
I learned XS&C++ from that tutorial and some examples I found on CPAN. I don't recall what I looked at then. But now I can point to my own work as a (good or bad, I don't know) example: Math::SymbolicX::FastEvaluator.
Similarly, the planned XS++ distribution contains a complete (albeit pointless) example of using XS++ to interface C++ and Perl. Since XS++ is translated to plain XS, you can use it to generate examples.
PS: There's also the Inline::CPP module. If that works, it is probably the easiest solution. I doubt it can handle templates, though.

Check http://www.swig.org :
"SWIG is a software development tool
that connects programs written in C
and C++ with a variety of high-level
programming languages. SWIG is used
with different types of languages
including common scripting languages
such as Perl, PHP, Python, Tcl and
Ruby."

I would normally choose XS, like tsee, but there is also Inline::C (or Inline::CPP in this case). I dislike SWiG and tend to avoid packages built around it.

Related

How to bind D and C

Can someone point me to a good tutorial of how to call C code from D ( and possibly to call a FORTRAN library from D)?
I've researched the topic online and keep finding incomplete examples. I would like someone to spell it out for me one time. For example, show some example bindings and explain the steps to compiling and linking them to work together.
I have both the DMD and GDC compilers on windows.
Some background: Programming isn't my main job, but I do it A LOT at work. I work with Python and Java a lot, my first language was C, and I've done some web programming too. For my next project I'd like to use D, as it seems the learning curve is much lower than C++. Also I want a compiled language, and I've never mixed two languages before.
have a look to Derelict.org, it contains dozen of C bindings using ffi and dynamic-linking (run-time loading and delegate assignation.)
Deimos is a huge repository of bindings and wrappers of C libraries to D. I suggest you take a look at some small binding, and see how people did it.

Lisp Interpreter in a C++ Program

I'm not sure I'm phrasing this right, but I'm pretty sure I'm looking for a LISP interpreter I can put in my C++ program.
The ideal situation I'm imagining is a function or something to which I can pass either a string, file, or filename containing the LISP code and then use the output from the LISP code in other parts.
To put it in terms of (print (eval (read))), I want read to be something I input to be what's read, and a string or something I can parse from print.
There is also ECL ("Embeddable Common Lisp"). It has the advantage, that it provides the full Common Lisp standard. Unfortunately, the documentation with respect to embedding is... well... a little bit scarce.
I never used it myself, so I cannot really tell, whether this would actually be an easy thing to embed into your application. IMHO, the Guile interpreter would be a reasonable choice.
Another embeddable Lisp is Rep, which is, for example, the extension language used by the Sawfish window manager. It started as Emacs lisp clone, but became something different over time, and is nowadays closer to Scheme.
If you're interested in Common Lisp, there's ECL.
From the FAQ:
2.1 What does this "embedding" stuff mean?
ECL is a full fledge
implementation of the Common-Lisp
language. However, due to the way it
is implemented, the implementation can
be used as an extensibility language
for your own application, much like
Guile works for the Scheme language.
By a rather simple set of functions,
you can parse, compile and execute
Common-Lisp forms, and using the
Foreign Function Interface (FFI), you
can add new functions to Common-Lisp
which suit your Domain-Specific
Language.
And:
2.2 How do I embed ECL into my application?
You have to use the ECL library, which
is called libecl.so, libecl.dyld or
ecl.dll, depending on your operating
system (Unix, Mac OSX or Windows). The
program ecl-config will provide you
with the flags you have to pass to the
C/C++ compiler and to the linker,
using either ecl-config --cflags or
ecl-config --ldflags, respectively.
Regarding your program, apart from
linking against the ECL library, you
have to call the proper initialization
routine, cl_boot(), so that ECL sets
up the appropiate internal structures.
After calling this routine, you will
be able to run lisp code and create or
manipulate lisp data
.
It's not Lisp, but Guile is the GNU cross platform scripting language, and is an interpreter/compiler for Scheme, which is pretty close to Lisp.
You can use GNU Guile, which is a Scheme interpreter specifically designed to be easily embeddable in C/C++ programs.
Try anything that comes up under this search: http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=embedded+lisp
It turns out that a lot of people have wanted to do this.
For something even more embeddable than Guile, there's also Chibi.
(I <3 Guile BTW, so this answer isn't trying to take away from that---it just offers another option.)

Good cross platform functional language for library use in a C++ application?

What are my options in terms of a fast functional language for library use in a cross-platform Qt C++ application?
It seems almost all languages (functional or not) have some way of calling C/C++ code in an easy manner. I'd like to go the other way around - write an application in Qt using C++ for stateful business logic, GUIs and stuff but drop down and use a functional language for the core calculation library.
Which ones are easy to use in this manner? Can for instance OCaml code be compiled into a static library and then consumed by a C++ app?
Thanks,
Rickard
Haskell has this capability, though the interaction with Qt/qmake and your build process with ghc may take a little trickery to get working:
http://www.haskell.org/haskellwiki/Calling_Haskell_from_C
There is also a project called HaskellDirect which seems similar to your purpose as well:
http://www.haskell.org/hdirect/
AutoCAD uses AutoLisp so my suggestion would be Lisp.
I'd be tempted to check out qtHaskell and to do the whole thing in Haskell. My opinion is based on Don Stewart's remarkable success doing xmonad in Haskell.
Lisp and Haskell are excellent functional languages but if we consider the ease of binding C/C++ code along with the language, I'd recommend lua.
It is extremely straightforward to bind C functions to lua right off the bat, the interpreter is super compact and easy library to build, it's among the fastest scripting languages out there, and, with luabind, you can easily bind C++ classes, template instantiations, etc. I've had to do bindings for numerous scripting languages in the past and I've never found one that's quite as straightforward as lua. It's also supported with swig if you prefer to bind things through swig which will allow your application to support multiple scripting languages.
From a pure language point of view, the meta-feature/metaprogramming aspect of lua (comparable to lisp) makes it very easy to support all kinds of programming paradigms, though I personally find it best suited for functional programming. It's extremely customizable and well-suited for embedded use.
However, since you are already using qt, qtHaskell might be a nice choice to consider as well.

Any high-level languages that can use c libraries?

I know this question could be in vain, but it's just out of curiosity, and I'm still much a newb^^ Anyways I've been loving python for some time while learning it. My problem is obviously speed issues. I'd like to get into indie game creation, and for the short future, 2d and pygame will work.
But I'd eventually like to branch into the 3d area, and python is really too slow to make anything 3d and professional. So I'm wondering if there has ever been work to create a high-level language able to import and use c libraries? I've looked at Genie and it seems to be able to use certain libraries, but I'm not sure to what extent. Will I be able to use it for openGL programing, or in a c game engine?
I do know some lisp and enjoy it a lot, but there aren't a great many libraries out there for it. Which leads to the problem: I can't stand C syntax, but C has libraries galore that I could need! And game engines like irrlicht. Is there any language that can be used in place of C around C?
Thanks so much guys
Python can call functions in dynamically loaded C libraries (.so in unix, .dll in Windows) using the ctypes module.
There is also cython - a variation of python that compiles to C and can call C libraries directly. You can mix modules written in pure Python and cython.
You may also want to look at the numerous 3D game engines either written specifically for Python or with a python interface. The ones I have heard the most about (but not used) are Blender and Python-Ogre.
Panda3D is an engine which uses Python as it's "game logic" interface. You basically write everything in Python and the Panda3D backend (which I assume is mostly written in C or C++) is responsible for rendering.
Check out the gallery of projects that use Panda3D. It's not going to be AAA the-next-Gears-of-War level graphics, but it's still pretty impressive.
Using swig you can make C imports in various languages: lua, python, php, c# ...
See more info here about supported wrappers.
Python is able to use C libraries via the ctypes module. You'll have to write some Python code to import the C functions, but if the C API is clean and simple you'll have no trouble at all.
You might find these useful:
C functions from Python
Integrating Python, C and C++
I have been using PyOpenGL, it works great. Swig does its job if you want to call C/C++ libraries from Python.
I'm surprised that no-one has yet stated clearly that C++ is what you are looking for. Like you I have a distaste for C syntax, but that's a poor reason for avoiding C++ if you want to get into 3D gaming. Do you want to dive into 3D gaming, or do you want to sit on the edge of the pool crying that the water is too cold ?
I think you'll also find that C++ plays very well with OpenGL, which is probably not true of a lot of the alternatives that have already been suggested
To some extent, Cython might be what you are looking for. It allows you to use Python as a high level language, but then use C for the parts that need to be optimized.
But, at the end of the day, if you want to do 3D, just learning C or C++ may be the way to go. :-)
There are Python wrappers available for major open source game engines (Ogre, Irrlicht, etc.). Particularly Panda3D ought to have nice bindings.
If you'd like to have a look at .Net platform. You have the following solution:
Use C++/CLI to compile your C/C++ code into .Net assembly, the running time of this part would be as the same as your native C/C++ code.
Use any .Net language (C#, F#, IronPython) to develop high-level stuff using the low level library. For pure number crunching, C#/F# is usually 2-4 times slower than native C code, which is still far faster than Python. For non-number crunching tasks, C#/F# could sometimes match the speed of native code.

Using Perl with compiled C library?

I would like to try and use Perl, but need to communicate with another application I wrote. I have an interface library that I wrote in C++, and it contains the socket communications and interface protocol to set/get parameters in my application.
I would like to use Perl with Apache to serve up web pages for configuring my application. I need to know how to get Perl to talk to my interface library.
Oh Lord, if you are new to Perl. you do not want to be looking at XS (I am not new to Perl and I do not want to look at XS). See Inline::C or Inline::CPP for a much gentler introduction to calling C and C++ from Perl. Drilling deeper into the XS interface should only be necessary if you want to start passing or returning complicated data structures around (and maybe not even then).
use Inline C => Config => LIBS => '-L/<yourlibpath> -l<yourlib>';
$x = my_library_function_that_returns_an_int_or_double($integer_arg,$string_arg);
It looks like you want to communicate with a C library from Perl. You'll want to read through perldoc perlxs and perldoc perlxstut for more about the XS interface. If you want to do the reverse (call Perl from C), also read through perldoc perlguts and perldoc perlembed.
When you encounter specific problems, give SO another holler and someone can surely help you out. :)
If you chose not to go the inline-C route, Perl has built in support for sockets, A great tutorial on doing IPC, and a few objects to help with sockets in general (plus, google will provide you many other tutorials on using perl sockets). If you're able/willing to re-implement the sockets communication part, you may not even need the C/C++.
You need to create a module for you C++ library, if you don't want to use Inline::CPP. It can be done automatically with h2xs: h2xs --autogen-xsubs. Install C::Scan from CPAN first.
First, let me say that XS a relatively small set of keywords that makes wrapping C libraries for use from perl easier (see perlxstut and perlxs). In order to use it, however, you will have to learn the perl API (see perlapi) which in turn requires a shallow knowledge of how the perl compiler works (see perlguts and the somewhat outdated but still highly useful perl guts illustrated). That's a lot to learn at the same time. In short:
XS itself => not hard
perlapi => moderately hard
perl inner workings => pretty hard
all of the above at the same time => ouch
That being said, if you want to wrap a C++ library, have a look at the ExtUtils::XSpp module on CPAN. It comes with a very simple example. The module's not that old and still being improved, but it makes wrapping C++ a lot more convenient.
PS: If you plan to learn everything at once, learn by examples. Check out Dean Roehrich's cookbooks. Maybe use some simple XS modules as models: Parse::ExuberantCTags, Math::FFTW or one of the many others on CPAN.