【问题标题】:Raku and cupsGetDests乐和杯子GetDests
【发布时间】:2020-12-05 22:56:22
【问题描述】:

Fedora 33

我正在尝试使用 Raku 从 cupsGetDests2 输出打印机列表。

这是 C 方式,虽然它也显示已删除的打印机:

#include <iostream> 
#include <cups/cups.h> 

int main() { 
cups_dest_t* dests; 
int nCount = cupsGetDests2(CUPS_HTTP_DEFAULT, &dests); 

for (int i = 0; i < nCount; i++) { 
   cups_dest_t dest = dests[i]; 
   std::cout << dest.name << std::endl; 
   } 
} 


$ list-printers 
B4350 
Cups-PDF 
Cups_PDF_rn6                <-- deleted 
Oki_B4350_on_dev_lp0_rn6    <-- deleted 
Virtual_PDF_Printer 
Virtual_PDF_Printer_rn6     <-- deleted

我添加了“

Raku 邮件列表上的一位朋友向我展示了这段代码:

#!/usr/bin/env raku 

use NativeCall; 
class CupsDest is repr('CStruct') { 
   has Str $.name;      # This is the first field in the struct -- add more if you need them 
} 

sub cupsGetDests(Pointer is rw --> int32) is native('cups') {} 

my $ptr = Pointer.new; 
my $nCount = cupsGetDests($ptr); 

for ^$nCount -> $i { 
    my $dest = nativecast(CupsDest, Pointer.new($ptr + $i * 
nativesizeof(Pointer))); 
    say $dest.name; 
} 

但是这个输出出错了:

$ ListPrinters.pl6 
Cannot locate native library 'libcups.so': libcups.so: cannot open shared object file: No such file or directory 
 in method setup at /opt/rakudo-pkg/share/perl6/core/sources/947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B (NativeCall) line 298 
 in block cupsGetDests at /opt/rakudo-pkg/share/perl6/core/sources/947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B (NativeCall) line 587 

libcups 真的存在!

$ locate libcups.so 
/usr/lib/libcups.so.2 
/usr/lib64/libcups.so.2 

我做错了什么?

非常感谢,

新信息:

我做了以下事情:

# cd /usr/lib
# ln -s libcups.so.2 libcups.so
# ls -al libcups*
-rwxr-xr-x. 1 root root  14612 Nov 10 06:07 libcupsimage.so.2
lrwxrwxrwx. 1 root root     12 Dec  6 01:03 libcups.so -> libcups.so.2
-rwxr-xr-x. 1 root root 710236 Nov 10 06:07 libcups.so.2

# cd /usr/lib64
# ln -s libcups.so.2 libcups.so
# ls -al libcups*
lrwxrwxrwx. 1 root root     23 Nov 23 22:09 libcupsfilters.so.1 -> libcupsfilters.so.1.0.0
-rwxr-xr-x. 1 root root 264440 Nov 23 22:09 libcupsfilters.so.1.0.0
-rwxr-xr-x. 1 root root  15256 Nov 10 06:08 libcupsimage.so.2
lrwxrwxrwx. 1 root root     12 Dec  6 01:04 libcups.so -> libcups.so.2
-rwxr-xr-x. 1 root root 686128 Nov 10 06:08 libcups.so.2

现在运行程序给出:

$ ListPrinters.pl6 
B4350
(Str)
Segmentation fault (core dumped)

这是我的打印机列表:

$ lpstat -a
B4350 accepting requests since Thu 29 Oct 2020 01:36:30 PM PDT
Cups-PDF accepting requests since Tue 30 Apr 2019 04:05:39 PM PDT
Virtual_PDF_Printer accepting requests since Tue 29 Sep 2020 03:13:17 AM PDT

现在我做错了什么?

在 Curt 的帮助下解决了。我的新代码:

#!/usr/bin/env raku

use NativeCall;

class CupsDest is repr('CStruct') {
    has Str $.name;
    has Str $.instance;
    has int32 $.is-default;
    has int32 $.num-options;
    has Pointer $.options;
}

sub cupsGetDests(Pointer is rw --> int32) is native('cups', v2) {}

my $ptr = Pointer.new;
my $nCount = cupsGetDests($ptr);

for ^$nCount -> $i {
    my $dest = nativecast(CupsDest, Pointer.new($ptr + $i * nativesizeof(CupsDest)));
    print "<" ~ $dest.name ~ ">\n";
}

输出:

$ ListPrinters.pl6
<B4350>
<Cups-PDF>
<Cups_PDF_rn6>
<Oki_B4350_on_dev_lp0_rn6>
<Virtual_PDF_Printer>

【问题讨论】:

  • 它没有通过它所指示的确切名称找到库。您必须将其中一个版本符号链接到 libcups.so 的合适(且可找到)目录中。
  • “无法找到本机库 'libcups.so'” 这对我来说没有发生。我在 Ubuntu 20.04 上试过这个,库由包libcups2-dev 提供并安装在/usr/lib/x86_64-linux-gnu/libcups.so,它在这里找到了库。
  • 如果一个答案解决了你的问题,你应该接受它。

标签: raku


【解决方案1】:

您已经获得了指向普通.so 的符号链接,但您也可以将is native('cups') 更改为is native('cups', v2) 以使其使用.so.2 库。

@jjmerelo 有正确的答案——我以为它是一个指针数组,但它实际上是一个结构数组。

填写整个Struct:

class CupsDest is repr('CStruct') {
    has Str $.name;
    has Str $.instance;
    has int32 $.is-default;
    has int32 $.num-options;
    has Pointer $.options;
}

并改变这一行:

my $dest = nativecast(CupsDest, Pointer.new($ptr + $i * nativesizeof(CupsDest)));

【讨论】:

  • 嗨,Curt,完美。比你!我会将我的新代码添加到原始帖子的底部。我对 v2 的事情一无所知。
  • 由于我的粗心,我最初遗漏了 num-options -- 只是添加了它。
  • 谢谢!不知道你为什么输入它。
【解决方案2】:

看着the definition of the function you're calling,弹出了一些东西

  • dests 的结构可能比您在此处描述的结构更复杂。除了name,还包括了一些其他的字段,比如instance
  • 您使用的字节数对应于通用指针的大小,而不是保存这种结构的数据结构的大小。我会说它有三个指针,加上几个整数。正确定义后,您可能需要使用 nativesizeof(CupsDest)

【讨论】:

  • 关于如何做到这一点的任何提示?
  • 我删除了符号链接。特意想用v2的东西,下次记起来了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-12
  • 2011-04-16
相关资源
最近更新 更多