[Development] Update on C bindings idea

samuel ammonius sfammonius at gmail.com
Fri Jan 13 02:38:48 CET 2023


Hello,

About a year ago, I emailed here asking if C bindings could be added to
Qt's official repo, and since then I've written a pretty large python
script that generates the wrappers by reading Qt's header files. I
originally thought that "inline extern" function declarations would make
the compiler copy the binary code of the function into the caller program,
but I just found out that the compiler ignores "inline" in this case. I
don't think that makes the idea of C wrappers useless, but there's another
method of binding to C that could dodge the performance issue, and maybe
even make Qt run faster on C++ (or at least compile faster). It does
interfere with Qt's existing codebase way more than the wrapper method
though, so I understand if it wouldn't be accepted.

The idea is to give Qt functions C-compatible names, and then wrap them
with "inline" functions inside a class. This code outlines the idea:


> // qpushbutton.h


> struct QPushButton_struct {
>
> bool flat;
>
> }
>
> void QPushButton_setFlat(QPushButton *btn, bool flat);
>
> bool QPushButton_flat(QPushButton *btn);
>
> #ifdef __cplusplus
>
> class QPushButton : private QPushButton_struct {
>
> public:
>
> Q_ALWAYS_INLINE auto setFlat(bool flat){ QPushButton_setFlat(this, flat); }
>
> Q_ALWAYS_INLINE auto flat(){ QPushButton_flat(this); }
>
};

#else
>
> typedef QPushButton_struct QPushButton;
>
> #endif
>

Making inline C++ wrappers for C is possible since C++ can read
"QPushButton_setFlat", but C can't read "QPushButton::setFlat". This change
isn't as drastic as it may seem at first, since all code inside the
functions can still use the C++ function names without any difference in
behavior or performance. The only change will be in the names of function
declarations and definitions, and in how classes are formed. Qt could still
be compiled if this is done to certain classes but not others, so the
transition wouldn't have to be instant.

This could also have benefits for Qt in C++. For example, since template
functions would no longer have linkage, Qt and the apps using it could have
faster compile times and take less space. I tested the idea thoroughly this
time before asking if it could be added, so I don't think there will be a
similar situation to the last attempt. As before, I would be happy to do
the whole initial transition by myself, and I'm just asking to see if this
change would be welcome. Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20230112/5978ce38/attachment.htm>


More information about the Development mailing list