首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >perl6 NativeCall在达尔文上找不到库

perl6 NativeCall在达尔文上找不到库
EN

Stack Overflow用户
提问于 2019-02-20 14:55:31
回答 1查看 208关注 0票数 6

为了熟悉Perl6的这一方面,我正在尝试使用NativeCall。当然,我正在尝试首先加载libstatgrab (还有什么?)。

因此,我从最简单的部分开始-主机信息。因为还没有集群支持,所以这只是一个结果--不用担心复杂性。

代码:

代码语言:javascript
复制
#!/usr/bin/env perl6 

use v6;
use NativeCall;

enum sg_error (
        SG_ERROR_NONE                   => 0,
        SG_ERROR_INVALID_ARGUMENT       => 1,
        ...
);

class sg_error_details is repr('CStruct') {
        has int32 $.error;
        has int32 $.errno_value;
        has Str $.error_arg;
};

sub sg_init(int32 $ignore_errors) returns int32 is native('statgrab') { * };

enum sg_host_state (
        sg_unknown_configuration        => 0,
        sg_physical_host                => 1,
        sg_virtual_machine              => 2,
        sg_paravirtual_machine          => 3,
        sg_hardware_virtualized         => 4
);

class sg_host_info is repr('CStruct') {
        has Str $.os_name;
        has Str $.os_release;
        has Str $.os_version;
        has Str $.platform;
        has Str $.hostname;
        has uint32 $.bitwidth;
        has int32 $.host_state;
        has uint32 $.ncpus;
        has uint32 $.maxcpus;
        has uint64 $.uptime;
        has uint64 $.systime;
};

sub sg_get_host_info(size_t is rw) returns Pointer is native('statgrab') is symbol('sg_get_host_info_r') { * };
sub sg_free_host_info(Pointer) is native('statgrab') is symbol('sg_free_stats_buf') { * };

sub MAIN() {
    my int32 $ignore_errors = 0;
    my $error = sg_init($ignore_errors);
    if $error != SG_ERROR_NONE {
        say "Maeh: $error";
        exit 1;
    }

    my size_t $num_host_infos = 0;
    my $res = sg_get_host_info($num_host_infos);
    if $num_host_infos > 0 {
        my $host_info = nativecast(sg_host_info, $res);
        with $host_info {
            say "You're using ", $_.os_name, " on ", $_.hostname;
        }
    }
    sg_free_host_info($res);
}

启动它(dumb)会导致加载库错误:

代码语言:javascript
复制
$ perl6 statgrab.p6
Cannot locate native library 'libstatgrab.dylib': dlopen(libstatgrab.dylib, 1): image not found
  in method setup at /Users/sno/rakudo/share/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03 (NativeCall) line 283
  in method CALL-ME at /Users/sno/rakudo/share/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03 (NativeCall) line 570
  in sub MAIN at statgrab.p6 line 95
  in block <unit> at statgrab.p6 line 93

好的-给它一些搜索路径:

代码语言:javascript
复制
$ LD_LIBRARY_PATH=/opt/pkg/lib:$LD_LIBRARY_PATH perl6 statgrab.p6
Cannot locate native library 'libstatgrab.dylib': dlopen(libstatgrab.dylib, 1): image not found

在使用DYLD_LIBRARY_PATH时也是如此-这也是由达尔文上的dlopen(3)支持的。

但在目录中进行更改会起作用:

代码语言:javascript
复制
$ (cd /opt/pkg/lib && perl6 /data/Projects/OSS/p6-Unix-Statgrab/statgrab.p6 )
You're using Darwin on ernie.[...]

在moarvm的调用方式中是否缺少搜索路径直通?

EN

回答 1

Stack Overflow用户

发布于 2019-03-24 00:29:49

代码语言:javascript
复制
doug$ perl6 -v
This is Rakudo Star version 2018.10 built on MoarVM version 2018.10
implementing Perl 6.c.

在最近在MacOS High Sierra上的Rakudo Star上,脚本对我来说是“开箱即用”的:

missing!)

  • brew install libstatgrab

  • Script
  1. 已编辑脚本以删除‘...’。
  2. 脚本无法加载库(实际上missing!)
  3. brew install libstatgrab
  4. Script已运行successfully:

代码语言:javascript
复制
vader:learning doug$ perl6 nativecall_mac_Sno.pl6 
You're using Darwin on Vader.local

Homebrew按如下方式安装了该库:

代码语言:javascript
复制
$ v /usr/local/lib
total 11904
-rw-r--r--  1 doug  admin  6080828 Sep 23 12:40 libmoar.dylib
lrwxr-xr-x  1 doug  admin       51 Mar 23 11:02 libstatgrab.10.dylib@ -> ../Cellar/libstatgrab/0.91/lib/libstatgrab.10.dylib
lrwxr-xr-x  1 doug  admin       44 Mar 23 11:02 libstatgrab.a@ -> ../Cellar/libstatgrab/0.91/lib/libstatgrab.a
lrwxr-xr-x  1 doug  admin       48 Mar 23 11:02 libstatgrab.dylib@ -> ../Cellar/libstatgrab/0.91/lib/libstatgrab.dylib
drwxr-xr-x  3 doug  admin      102 Mar 23 11:02 pkgconfig/

对我来说,perl6可执行文件确实是一个shell脚本,但它是有效的(不需要传递任何额外的LD_LIBRARY_PATH=...)。

代码语言:javascript
复制
doug$ file `which perl6`
/Applications/Rakudo/bin/perl6: POSIX shell script text executable, ASCII text, with very long lines
doug$ set | grep LIBRARY
doug$

我也遇到过原生脚本查找库的问题,但我总是通过修复库安装和/或提供'LD_ library _PATH‘来解决这些问题。

很抱歉,这段经历对你来说不是很棒

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54780504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档