【发布时间】:2019-07-13 19:09:38
【问题描述】:
我正在使用 NativeCall 来熟悉 Perl6 的那一面。当然,我正在尝试先加载 libstatgrab(还有什么?)。
所以我从最简单的部分开始 - 主机信息。由于还没有集群支持,这只是一个结果 - 不用担心复杂性。
代码:
#!/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);
}
启动它(哑)会导致加载库错误:
$ 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
好的 - 给它一些搜索路径:
$ 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 时的相同图片 - Darwin 上的 dlopen(3) 也支持。
但更改目录有效:
$ (cd /opt/pkg/lib && perl6 /data/Projects/OSS/p6-Unix-Statgrab/statgrab.p6 )
You're using Darwin on ernie.[...]
moarvm的调用方式是否缺少搜索路径直通?
【问题讨论】:
-
我在 Linux 上,所以我不确定我能不能在这里提供帮助,但在 Mac 上,
/opt/pkg/lib目录的内容是什么?文件libstatgrab.dylibpresent 吗? -
好的,从
enum中删除带有...的行后,您的脚本在Ubuntu 上运行良好。我从运行脚本得到的输出:You're using Linux on hakon-Precision-7530 -
回答您的第一个问题:是的。请使用更改目录命令查看最后一个代码片段。是的 - 我缩短了代码,因为堆栈溢出归咎于我,我的问题包含很多代码;)最后你说:在 Linux 上工作,所以这可能是达尔文唯一的问题?
-
我不了解达尔文或一般的系统知识,但是,这不是 P6/MoarVM。这些 SO 是否相关? Error: dlopen() Library not loaded Reason: image not found? Alternative for the DYLD_LIBRARY_PATH-trick since Mac OS 10.11 El Capitan with System Integrity Protection?另外,
LD_DEBUG=all有用吗? -
@Sno perl6 可能会在下一个版本之后切换到使用小型二进制文件而不是当前的 shell 脚本。
标签: shared-libraries raku dlopen nativecall