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
我添加了“<--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
现在我做错了什么?
在科特的帮助下解决了。我的新代码:
#!/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>
发布于 2020-12-08 04:16:18
您已经获得了指向普通.so
的符号链接,但是也可以将is native('cups')
更改为is native('cups', v2)
,使其使用.so.2
库。
@jjmerelo有正确的答案--我以为它是一个指针数组,但实际上它是一个结构数组。
填写整个结构:
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)));
发布于 2020-12-06 19:29:48
看看the definition of the function you're calling,有几件事浮出水面
dests
可能有一个比您在这里描述的更复杂的结构。除了name之外,它还包括一些其他字段,比如instance
.nativesizeof(CupsDest)
,您可能需要使用它。https://stackoverflow.com/questions/65162829
复制相似问题