[Development] Could support for C be added to Qt?
samuel ammonius
sfammonius at gmail.com
Sat Sep 10 00:05:10 CEST 2022
Hello,
I'm new to contributing to Qt directly, but I've been trying to create
external C bindings for Qt for a few months now. Since C and C++ are so
similar, I've recently been thinking it may just be better to add C support
directly to the Qt headers by checking for the __cplusplus macro.
The design I was thinking of in particular was to put a function macro in
qtglobals.h for creating C aliases for C++ functions (I'll call the macro
QT_C_EXPORT for now).
#ifdef __cplusplus
# define QT_C_EXPORT(RETURN_TYPE, A, B, PARAMS) extern "C" const auto B
#else
# define QT_C_EXPORT(RETURN_TYPE, A, B, PARAMS) RETURN_TYPE B PARAMS
#endif
#define QT_DEFINE_C_EXPORT(RETURN_TYPE, A, B, PARAMS) extern "C" const auto
B = &##A
Then it can be used like this:
________________________________________________________________
[qpushbutton.h]
#ifdef __cplusplus
class QPushButton {...};
#else
typedef struct QPushButton QPushButton;
QT_C_EXPORT(void, QPushButton::setFlat, QPushButton_setFlat, (bool));
[qpushbutton.cpp]
// functions...
QT_DEFINE_C_EXPORT(void, QPushButton::setFlat, QPushButton_setFlat, (bool));
________________________________________________________________
There's a couple things I still haven't figured out though, like
constructors/destructors and templates. Are there solutions to these that
would still allow support to be added to Qt internally or would external
bindings be better?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20220909/765d91cd/attachment.htm>
More information about the Development
mailing list