我在试着制作一个杯子打印系统。我想得到打印机的状态,到目前为止打印了多少页等等。
为此,我正在执行CUPS示例中给出的示例程序。
#include <cups/cups.h>
#include <stdio.h>
int main(){
int num_options;
cups_option_t *options;
cups_dest_t *dests;
int num_dests = cupsGetDests(&dests);
cups_dest_t *dest = cupsGetDest("name", NULL, num_dests, dests);
int job_id;
/* Print a single file */
job_id = cupsPrintFile(dest->name, "testfile.txt", "Test Print", num_options, options);
cupsFreeDests(num_dests, dests);
return 0;
}我使用gcc myfile.c -o myout -lcups编译它
当我尝试执行./myout时
我得到了
分段故障
我使用树莓-π3板作为我的CUPS服务器。
提前谢谢。
发布于 2017-11-28 07:28:02
dest指向无效地址.
cups_dest_t *dest; // declared but not initialized or assigned afterwards 所以去引用它(cupsPrintFile(dest->name.)是UB,可以导致SegFault。
这是您应该如何使用它(摘自这里):
#include <cups/cups.h>
cups_dest_t *dests;
int num_dests = cupsGetDests(&dests);
cups_dest_t *dest = cupsGetDest("name", NULL, num_dests, dests);
/* do something with dest */
cupsFreeDests(num_dests, dests);更新:
您的代码不处理某些变量(即不初始化-坏)。我看到的第一个是cups_option_t *options;。照顾好所有的变量,如果不起作用的话-调试。
int main(){
int num_options;
cups_option_t *options; // add a call to "cupsAddOption("first", "value", num_options, &options);"
cups_dest_t *dests;
int num_dests = cupsGetDests(&dests);
cups_dest_t *dest = cupsGetDest("name", NULL, num_dests, dests);
int job_id;
/* Print a single file */
job_id = cupsPrintFile(dest->name, "testfile.txt", "Test Print", num_options, options); // options is used here but is uninitialized
cupsFreeDests(num_dests, dests);
return 0;
}https://stackoverflow.com/questions/47526085
复制相似问题