2

是否有ExtUtils::*Module::Build(或其他)类似于 Ruby 的mkmf.have_struct_member

我想做类似的事情(以提示/文件的方式):

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

Config.pm不跟踪我正在寻找的具体信息,ExtUtils::FindFunctions在这里似乎不太合适......

4

2 回答 2

3

我知道这不是 MakeMaker 或 Module::Build 内置的。CPAN 上可能有一个东西可以做到这一点,但通常的方法是使用 ExtUtils::CBuilder 编译一个小测试程序并查看它是否运行。

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 };

可能想在临时文件中执行此操作并在它之后进行清理等...

于 2010-01-22T03:46:54.983 回答
1

我写了一个包装器ExtUtils::CBuilder来做“这个C代码编译吗?” Build.PL在或Makefile.PL脚本中输入测试,称为ExtUtils::CChecker

例如,您可以通过以下方式轻松测试上述内容:

use Module::Build;
use ExtUtils::CChecker;

my $cc = ExtUtils::CChecker->new;

$cc->try_compile_run(
    define => "TRY_ACCRIGHTS_NOT_CMSG",
    source => <<'EOF' );
      #include <sys/types.h>
      #include <sys/socket.h>
      int main(void) {
        struct msghdr cmsg;
        cmsg.msg_accrights = 0;
        return 0;
      }
EOF

$cc->new_module_build(
    configure_requires => { 'ExtUtils::CChecker' => 0 },
    ...
)->create_build_script;
于 2012-04-19T15:38:21.840 回答