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

hi!

a few days ago i was pointed to P5NCI::Library to interact with C-libraries. i had a closer look at it and it's really useful and great! but by playing around with it, a few problems came up. it seems that load_function does not accept a void-signature. i put up a test-library that takes various arguments. amongst them 'void'. here is the .c-code for the test-library:
#include <stdio.h> #include <string.h> char *hello(void) { static char result[]={"hello world"}; return result; } char *bye(char *str) { static char prefix[]={"good bye"}; static char result[32]; strcpy(result,prefix); strcat(result,str); return result; } char *return_char(int chr) { static char result[1]; sprintf(result, "%c", chr); return result; } int square(int x) { x = (x * x); return x; } int do_nothing(void) { return 1; }
and here is a little test-script using P5NCI::Library to interact with my test-library:
#!/usr/bin/perl use P5NCI::Library; my $lib = P5NCI::Library->new( library => 'hello_world', package => 'T +EST' ); my $test = $lib->install_function( 'hello', 'tv' ); my $hello = TEST::hello(); print "for hello() i got: $hello\n"; undef $test; my $test = $lib->install_function( 'return_char', 'ti' ); my $return_char = TEST::return_char( 65 ); print "for return_char(65) i got: $return_char\n"; undef $test; my $test = $lib->install_function( 'square', 'ii' ); my $square = TEST::square( 3 ); print "for square(3) i got: $square\n"; undef $test; my $test = $lib->install_function( 'bye', 'tt' ); my $bye = TEST::bye( "thanks for all the passwords" ); print "for bye(test) i got: $bye\n"; undef $test; my $test = $lib->install_function( 'do_nothing', 'vi' ); my $do_nothing = TEST::do_nothing(); print "for do_nothing() i got: $do_nothing\n"; undef $test;

the functions 'return_char', 'square', and 'bye' work just perfect, but 'hello' and 'do_nothing' do not. both have a void input-signature.
am i doing something wrong, or is P5NCI having troubles with these kind of functions? TIA for any hint.
br, nos

Replies are listed 'Best First'.
Re: P5NCI + void load_function-signature
by chromatic (Archbishop) on Jun 22, 2006 at 18:55 UTC

    After fixing the signature to iv, the code you posted works for me. Do you not have the symbol XS_P5NCI_nci_iv defined in your P5NCI.so? If so, I wonder if the generated XS file is incomplete.

      indeed a strings P5NCI.so | grep XS_P5NCI_nci_iv shows no hit. hm... the problem might be, that my P5NCI-module was not built via "perl ./Build", but rather by converting a prebuilt package to my needs. today in the morning (localtime, so about 12 hours ago) i tried to compile from tarball. but the build process failed after about an hour :(
      ok, so it seems my module seems broken. a fact i can live with.
      thanks a lot, chromatic for helping me on this one!
      br, nos

        You do need a fair amount of memory to compile the generated XS file. I could probably chunk it into separate files, so as not to choke compilers and machines without spare cycles.

        If you don't have the right signature, you do need to rebuild it though. (If you want to do that and don't need too much complexity, change my $max_len       = 5; on line 72 of build_lib/P5NCI/GenerateXS.pm. Reducing that to 4 should make a much smaller, if less capable, XS file.)

Re: P5NCI + void load_function-signature
by samtregar (Abbot) on Jun 22, 2006 at 15:35 UTC
    my $test = $lib->install_function( 'do_nothing', 'vi' );

    Shouldn't that signature be 'iv'?

    -sam

      yes, sam, you are right. i tried both, and seems i forgot to paste the latest version of my test-script.
      nonetheless, it still does not work :(
      Don't understand NCI signature 'iv' at /usr/share/perl5/P5NCI/Library.pm line 40
        The docs for P5NCI::Library say:

        This function itself will throw an exception if you fail to provide both a function name and its signature. It will also throw an exception if it does not understand the signature. That might not be an error -- it doesn't understand a lot of valid signatures yet.

        Looks like you found one it doesn't "understand."

        -sam

A reply falls below the community's threshold of quality. You may see it by logging in.