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.
You can also try swig. It's a tool for generating interfaces to several scripting languages from C/C++.
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.
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.
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.
© 2022 - 2024 — McMap. All rights reserved.