如何创建可以从 C 调用的基于 Qt 的库

发布于 2024-12-23 11:56:42 字数 486 浏览 0 评论 0原文

我正在尝试构建一个简单的库,它使用 Qt 的一些功能(没有事件循环,只是一些图像操作和文件加载),然后从标准 C 程序调用该库。我已在此处查看了答案,但答案没有为我提供足够的信息来解决问题。我已将该库构建为要链接的静态库,并使用 extern "C" 包装了函数调用,但我收到了一堆错误,从 /usr/include/QtCore 开始/qnamespace.h:-1: 在函数 'QT_MODULE':/usr/include/QtCore/qnamespace.h:54: 错误: 未知类型名称 'namespace'.

有没有人有关于链接到的简短教程来自 C 程序的 Qt 库?仅供参考,一切都在 Linux (Fedora 15) 下进行,并且 C 程序和 Qt 库都是使用 QtCreator 构建的。

I'm trying to build a simple library that uses some of Qt's functionality (no event loop, just some image manipulation and file loading) and then call that library from a standard C program. I've reviewed the answer here, but the answer did not provide enough information for me to resolve the problem. I have built the library as a static library to be linked, and wrapped the function calls with extern "C", but I get a bunch of errors, starting with /usr/include/QtCore/qnamespace.h:-1: In function ‘QT_MODULE’:/usr/include/QtCore/qnamespace.h:54: error: unknown type name ‘namespace’.

Does anyone have a short tutorial on linking to Qt libraries from a C program? FYI, everything is under Linux (Fedora 15), and both the C program and the Qt library are being built with QtCreator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水溶 2024-12-30 11:56:42

假设您想制作一个用于创建和删除 QImage 的包装器,可以这样做:

在您的头文件中:

typedef void *my_qimage_t;

extern "C" my_qimage_t my_create_qimage(int width, int height);
extern "C" void my_delete_qimage(my_qimage_t image);

在您的源文件中:

extern "C" my_qimage_t my_create_qimage(int width, int height)
{
    QImage *qimage = new Qimage(width, height);
    return static_cast<my_qimage_t>(qimage);
}

extern "C" void my_delete_qimage(my_qimage_t image);
{
    QImage *qimage = static_cast<QImage *>(image);
    delete qimage;
}

my_qimage_t 类型从调用者的角度来看,它是一个黑匣子。所有操作都必须通过您的库通过传递这个不透明的指针来完成。

Lets say you want to make a wrapper for creating and deleting QImage, it could be done something like this:

In your header file:

typedef void *my_qimage_t;

extern "C" my_qimage_t my_create_qimage(int width, int height);
extern "C" void my_delete_qimage(my_qimage_t image);

And in your source file:

extern "C" my_qimage_t my_create_qimage(int width, int height)
{
    QImage *qimage = new Qimage(width, height);
    return static_cast<my_qimage_t>(qimage);
}

extern "C" void my_delete_qimage(my_qimage_t image);
{
    QImage *qimage = static_cast<QImage *>(image);
    delete qimage;
}

The my_qimage_t type is, from the callers perspective, a black box. All manipulation has to be done through your library, by passing this opaque pointer around.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文