如何判断 C 结构体是否有 Perl XS 中的成员?

发布于 2024-08-18 17:44:13 字数 536 浏览 6 评论 0原文

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

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

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

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

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤带着心事 2024-08-25 17:44:13

我知道这没有内置到 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 };

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

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...

木緿 2024-08-25 17:44:13

我围绕 ExtUtils::CBuilder 编写了一个包装器,用于执行“此 C 代码是否可以编译?”在 Build.PLMakefile.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;

I wrote a wrapper around ExtUtils::CBuilder for doing "does this C code compile?" type tests in Build.PL or Makefile.PL scripts, called ExtUtils::CChecker.

For example, you can easily test the above by:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文