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

This is more of a curiosity thing than anything as I must admit, I don't know a whole lot of C.

Can someone explain what I'm missing in the below code? In the C num() function, I have a uint8_t argument, which causes an error:

Undefined subroutine &main::num called at perl.pl line 6.

The broken code:

use warnings; use strict; use Inline 'C'; num(1); __END__ __C__ #include <stdio.h> #include <inttypes.h> void num(uint8_t number){ printf("%d\n", number); }

However, if I accept the argument as an int, and then cast it to uint8_t, it works fine:

void num(int number){ number = (uint8_t)number; printf("%d\n", number); }

The code that breaks in Perl (uint8_t in the function definition) works fine when compiling/running it as a straight up C program.

Replies are listed 'Best First'.
Re: Inline::C Undefined subroutine with uint8_t C function parameter
by BrowserUk (Patriarch) on Jan 09, 2017 at 21:41 UTC

    The only C types that Inline::C automatically maps to & from are those found in yourperl\lib\ExtUtils\typemap.

    To use any types not found in there you need to create your own typemap file and add it to the build configuration. See C-Perl bindings section of the POD.

    (Also see the "typemaps" configuration option a few lines above the start of that section.)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Fantastic. Thanks BrowserUK!

      I do vaguely recall having to do this for another XS distribution I wrote quite a while ago, but must have forgotten.