Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

interface perl to C++ code with SWIG, re: void pointers?

by velVerlet1 (Initiate)
on Aug 26, 2017 at 06:29 UTC ( [id://1198051]=perlquestion: print w/replies, xml ) Need Help??

velVerlet1 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: interface perl to C++ code with SWIG, re: void pointers?
by syphilis (Archbishop) on Aug 26, 2017 at 09:15 UTC
    TypeError in method 'version', argument 1 of type 'void *' at ./test_swig.pl line 8

    You've specified in your C++ code that the version method takes a single argument, but when you call that method from perl you provide no argument at all.
    I don't really know what version() is supposed to do, but if the correct way to call it from perl is to provide no arguments, then the C++ code should also declare version() as taking no arguments:
    int version();
    or, if C++ complains about that, try:
    int version(void);
    UPDATE: Duh ... my stupidity ... $control->version() does, of course, pass one argument to the version function.

    Sorry - I have some experience with C and perl, but little experience with C++, and virtually no experience with SWIG.
    A more common approach to having perl access C++ code is via the perl module Inline::CPP (instead of SWIG).

    Cheers,
    Rob
Re: interface perl to C++ code with SWIG, re: void pointers?
by syphilis (Archbishop) on Aug 27, 2017 at 00:56 UTC
    After my initial gaff, I'll have another crack at providing something that might be marginally useful - and I second stevieb's request for more info.
    Inline::CPP and (Inline::C) seem to be quite happy to directly access the library's version() function: Here's the Inline::CPP demo:
    ################ package FOO; use strict; use warnings; use Inline CPP => Config => BUILD_NOISY => 1, ; use Inline CPP => <<'EOC'; int version(void * p) { return 42; } EOC my $sv = 'whatever'; my $obj = bless \$sv, 'FOO'; print $obj->version(); ################
    That works just fine and outputs '42'.
    Of course, it's a little bit different in your case because version() is in a separate library, so your script would be something like:
    ################ package FOO; use strict; use warnings; use Inline CPP => Config => INC => '/path/to/include', LIBS => '-L/path/to/library -lmy_lib', BUILD_NOISY => 1, ; use Inline CPP => <<'EOC'; #include <my_lib.h> int wrap_version(SV * p) { return version(p); } EOC my $sv = 'whatever'; my $obj = bless \$sv, 'FOO'; print $obj->wrap_version(); ################
    or, if you want to call the sub from perl as "version":
    ################ package FOO; use strict; use warnings; use Inline CPP => Config => PREFIX => 'wrap_', INC => '/path/to/include', LIBS => '-L/path/to/library -lmy_lib', BUILD_NOISY => 1, ; use Inline CPP => <<'EOC'; #include <my_lib.h> int wrap_version(SV * p) { return version(p); } EOC my $sv = 'whatever'; my $obj = bless \$sv, 'FOO'; print $obj->version(); ################
    I think SWIG should be capable of providing the same result.

    Cheers,
    Rob
Re: interface perl to C++ code with SWIG, re: void pointers?
by stevieb (Canon) on Aug 26, 2017 at 21:39 UTC

    Could you please post a minimalistic base of your C++ code that allows for the instantiation of an object, and at least one of its methods?

    That will allow us to prototype/test the code in different scenarios and get you an example working result.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1198051]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found