如何为 cairo_pdf_surface_create_for_stream() 创建正确的回调?
我需要将 PDF 输出到文件描述符,我正在考虑使用 Cairo 和 cairo_pdf_surface_create_for_stream()
:
我有一个写入函数,它具有以下签名:
static cairo_status_t cairowrite(void *fp, unsigned char const *data, unsigned int length);
它返回 CAIRO_STATUS_SUCCESS
>。
现在,我如何将此函数作为回调移交?
我尝试了这个:
cairo_write_func_t (*wfunc)(void *, unsigned char const *, unsigned int);
wfunc = (cairo_write_func_t)&cairowrite;
surface = cairo_pdf_surface_create_for_stream((cairo_write_func_t)wfunc, fp, (double)realwidth, (double)realwidth);
但我收到编译器警告:警告:来自不兼容指针类型的赋值
这位于我执行wfunc = &cairowrite;
的行上。
我做错了什么?
回调函数给出此 gcc 错误:cairo_status_t
,但必须是 cairo_write_func_t
类型。
使用 clang 我收到此错误:
qrencode.c:250:11: warning: incompatible pointer types assigning to 'cairo_write_func_t (*)(void *, unsigned char const *, unsigned int)' from
'cairo_write_func_t' (aka 'cairo_status_t (*)(void *, unsigned char const *, unsigned int)')
wfunc = (cairo_write_func_t)&cairowrite;
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
我应该做什么?
I need to output a PDF to a file descriptor, and I was thinking of using Cairo and cairo_pdf_surface_create_for_stream()
:
I have a write function, that has this signature:
static cairo_status_t cairowrite(void *fp, unsigned char const *data, unsigned int length);
It returns CAIRO_STATUS_SUCCESS
.
Now, how do I hand this function over as callback?
I tried this:
cairo_write_func_t (*wfunc)(void *, unsigned char const *, unsigned int);
wfunc = (cairo_write_func_t)&cairowrite;
surface = cairo_pdf_surface_create_for_stream((cairo_write_func_t)wfunc, fp, (double)realwidth, (double)realwidth);
But I'm getting compiler warnings: warning: assignment from incompatible pointer type
Which is on the line where I do wfunc = &cairowrite;
.
What am I doing wrong?
The callback function gives this gcc error: cairo_status_t
but has to be of type cairo_write_func_t
.
With clang I get this error:
qrencode.c:250:11: warning: incompatible pointer types assigning to 'cairo_write_func_t (*)(void *, unsigned char const *, unsigned int)' from
'cairo_write_func_t' (aka 'cairo_status_t (*)(void *, unsigned char const *, unsigned int)')
wfunc = (cairo_write_func_t)&cairowrite;
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用示例定义函数指针
:
输出:
You could try to define the function pointer with
Example:
Output:
问题解决了!
我有函数定义:
这就是我传递回调的方式:
我不再使用中间函数指针。 clang 和 gcc 不会给出任何错误。
Problem solved!
I have the function definition:
And this is how I pass the callback:
I don't use the intermediate function pointer anymore. clang and gcc don't give any errors.