首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Raku和cupsGetDests

Raku和cupsGetDests
EN

Stack Overflow用户
提问于 2020-12-06 06:56:23
回答 2查看 120关注 0票数 1

Fedora 33

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

这是C方式,尽管它也显示删除的打印机:

代码语言:javascript
运行
复制
#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邮件列表上的一个朋友给我看了这个代码:

代码语言:javascript
运行
复制
#!/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; 
} 

但是它错误地输出了以下输出:

代码语言:javascript
运行
复制
$ 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真的存在!

代码语言:javascript
运行
复制
$ locate libcups.so 
/usr/lib/libcups.so.2 
/usr/lib64/libcups.so.2 

我做错了什么?

非常感谢,

新信息:

我做了以下工作:

代码语言:javascript
运行
复制
# 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

现在运行该程序会产生以下结果:

代码语言:javascript
运行
复制
$ ListPrinters.pl6 
B4350
(Str)
Segmentation fault (core dumped)

这是我的打印机列表:

代码语言:javascript
运行
复制
$ 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

现在我做错了什么?

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

代码语言:javascript
运行
复制
#!/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";
}

输出:

代码语言:javascript
运行
复制
$ ListPrinters.pl6
<B4350>
<Cups-PDF>
<Cups_PDF_rn6>
<Oki_B4350_on_dev_lp0_rn6>
<Virtual_PDF_Printer>
EN

回答 2

Stack Overflow用户

发布于 2020-12-08 04:16:18

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

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

填写整个结构:

代码语言:javascript
运行
复制
class CupsDest is repr('CStruct') {
    has Str $.name;
    has Str $.instance;
    has int32 $.is-default;
    has int32 $.num-options;
    has Pointer $.options;
}

并更改此行:

代码语言:javascript
运行
复制
my $dest = nativecast(CupsDest, Pointer.new($ptr + $i * nativesizeof(CupsDest)));
票数 4
EN

Stack Overflow用户

发布于 2020-12-06 19:29:48

看看the definition of the function you're calling,有几件事浮出水面

  • dests可能有一个比您在这里描述的更复杂的结构。除了name之外,它还包括一些其他字段,比如instance.
  • You're使用的字节数对应于通用指针的大小,而不是保存这种结构的数据结构的大小。我会说它有三个指针,加上几个整数。一旦正确定义了nativesizeof(CupsDest),您可能需要使用它。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65162829

复制
相关文章

相似问题

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