[Development] Could support for C be added to Qt?

Kevin Kofler kevin.kofler at chello.at
Sun Sep 11 16:25:48 CEST 2022


samuel ammonius wrote:
> 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 main issue I see with your proposed approach is that it is very reliant 
on internals of the compiler you are using (I presume GCC and/or Clang), if 
it even compiles at all.

E.g., your QT_DEFINE_C_EXPORT macro expands to something like:
extern "C" const auto QPushButton_setFlat = &QPushButton::setFlat;
(By the way, I believe the ## operator is unnecessary there.)

I believe it is not guaranteed at all that a pointer-to-member can validly 
be exported as extern "C", nor how the this pointer is to be passed when you 
do that.

And the C declaration from QT_C_EXPORT expands to:
void QPushButton_setFlat(bool);
which does not include a this pointer anywhere, so where does the object on 
which to call setFlat come from?

The approach you currently implemented, with the wrapper functions, is much 
more portable, though indeed at the expense of an extra function call for 
every call to a member function (method) of a C++ object.

        Kevin Kofler



More information about the Development mailing list