【发布时间】: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