perlperl-xs

How can I tell if a C struct has a member in Perl XS?


Is there an ExtUtils::* or Module::Build (or other) analog to Ruby's mkmf.have_struct_member?

I'd like to do something like (in the manner of a hints/ file):

....
if struct_has_member("msghdr", "msg_accrights") {
    $self->{CCFLAGS} = join(' ', $self->{CCFLAGS}, "-DTRY_ACCRIGHTS_NOT_CMSG");    
}
...

Config.pm doesn't track the specific information I'm looking for, and ExtUtils::FindFunctions didn't seem quite appropriate here...


Solution

  • I know this is not built into either MakeMaker or Module::Build. There might be a thing on CPAN to do it, but the usual way is to use ExtUtils::CBuilder to compile up a little test program and see if it runs.

    use ExtUtils::CBuilder;
    
    open my $fh, ">", "try.c" or die $!;
    print $fh <<'END';
    #include <time.h>
    
    int main(void) {
        struct tm *test;
        long foo = test->tm_gmtoff;
    
        return 0;
    }
    END
    
    close $fh;
    
    $has{"tm.tm_gmtoff"} = 1 if
        eval { ExtUtils::CBuilder->new->compile(source => "try.c"); 1 };
    

    Probably want to do that in a temp file and clean up after it, etc...