Ciao Monks:

Newbie here, and trying to do a somewhat ambitious project, so apologies if this is somewhat stupid.
I'm trying to create a perl interface to my C++ finite element code.
The basic structure of the code is as follows:
I have an initiator function
void init(int argc, char **argv, void **ptr);
which creates a new instance of my class and assigns a pointer to it (ptr)

I have several member functions such as
int version(void *ptr);
which returns the various properties i'm interested in (in this case, ptr->version)

I've created a shared C++ library (mylib.so) already.

In order to create the perl interface, I used SWIG with the following interface file:

%include "exception.i" %exception { try { $action } catch (const std::exception &e) { SWIG_exception_fail(SWIG_RuntimeError, e.what()); } } %module mylib %{ /* Put headers and other declarations here */ #include "library.h" %} %include "std_vector.i" %include "std_string.i" %include "library.h"

Then I run swig and compile, which all goes well.
My question:
I want to do something like the following in perl

my $control = new mylib;
print "version: " . $control->version() . "\n";

However, whenever I try to access any of my member functions, the code complains:

TypeError in method 'version', argument 1 of type 'void *' at ./test_swig.pl line 8.

So basically, the function version expects a pointer (to self I assume???) but I'm not sure how to pass a void pointer to the C++ init function from perl
Any pointers (no pun intended) would be appreciated

Best,
Mike

In reply to interface perl to C++ code with SWIG, re: void pointers? by velVerlet1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.