tags:

views:

265

answers:

6
+5  Q: 

C++ to Perl/Tk

Hey,

I have to do a college project using C++. It also requires a GUI and I want to use Perl/Tk for the Gui, but am not sure how to link the C++ to the Perl. The project requires being able to pass variables back and forth. Could anyone point me in the direction of some good tutorials/books for linking the two? Or any ideas on how I should approach the problem as I have never had to link two languages before.

Thanks

+4  A: 

I'm working on a library to make that as simple as possible, but it's still an alpha version.

Leon Timmermans
+5  A: 

You can also try swig. It's a tool for generating interfaces to several scripting languages from C/C++.

David Nehme
+2  A: 

Since Perl is going to be providing the GUI, I'd embed the C++ code into Perl. Assuming that there's going to be a significant amount of C++ code, I'd put that into a library. The traditional way of linking that library to Perl is to create a Perl module using XS. The Tutorial for writing XSUBs and XS language reference manual will help with that.

A somewhat easier way may be to use the Inline module. I've used Inline::C before, and it worked well, but I've never tried Inline::CPP (the C++ version). I see it has mixed reviews.

cjm
A: 

if its a small project, its probably not worth the investment to spend lots of time getting the two languages to talk. you might consider using a more appropriate tool. C# will talk to C++ with a lot less pain.

Dustin Getz
+1  A: 

If I was tackling this problem I'd be using TCL/TK to create a GUI and then building a TCL extension in C/C++ that can be called from TCL/TK. This is one of the things that TCL/TK is really good at (other dynamic languages can do this as well but I like TCL/TK). You create a shared libray (.so on Unix or .dll on windows) with the C++ bits and they get imported as commands into TCL when you load the library.

Swig, which has already been mentioned, is a tool that helps automate the wrapping process, it can take your C++ code and create a wrapper that allows it to be loaded into a number languages such as TCL, Perl, Python, Ruby ...

Start at http://www.tcl.tk/ for lots of informtion an TCL and TK.

Jackson
A: 

Thanks for the answers they are exactly what I've been looking for.

Since Tcl/Tk was mentioned, we could need a language to call various C++ libraries. Would Tcl or Perl be better for calling these, or is that too broad a question? The project is score keeping based on real time imaging,so speed is a big factor.

You can use shared libraries from essentially every dynamic language, so you should choose one for different reasons. If speed (of your GUI!)) is of concern and you just consider Tcl and Perl, Perl's quite a bit faster.
tsee