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

Allan Sandfeld Jensen kde at carewolf.com
Sat Sep 10 00:12:20 CEST 2022


On Samstag, 10. September 2022 00:05:10 CEST samuel ammonius wrote:
> 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?

You could just compile your C programs with a C++ compiler, then you could use 
Qt as intended, and keep everything else C. While there are differences 
between C and C++ they are few a far between, so generally any C program is 
also a valid C++ program.

To me trying to wrap the API is just a way to do things more complicated, for 
the sake of making it more complicated.

Best regards
Allan






More information about the Development mailing list