5

I have to do a college project using C++ that requires a GUI. I want to use Perl/Tk for the GUI, but I 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.

Nathan Smith
  • 683
  • 1
  • 10
  • 24

5 Answers5

6

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

Leon Timmermans
  • 30,029
  • 2
  • 61
  • 110
6

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

David Nehme
  • 21,379
  • 8
  • 78
  • 117
5

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
  • 61,471
  • 9
  • 126
  • 175
  • Following-up years later. Inline::CPP hadn't been maintained since 2003. In 2011 I started working on it as a co-maintainer, and committed about 135 patches between Nov-2011 and May-2012. It's in much better shape now. The RT bug list is down to one lingering wishlist item. And the CPAN Testers install success ratio is up to the mid-90% range. – DavidO Jul 24 '12 at 00:16
1

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
  • 5,627
  • 2
  • 29
  • 48
0

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
  • 21,282
  • 15
  • 82
  • 131