开发者

Use a Qt library from a C application

开发者 https://www.devze.com 2023-03-16 16:44 出处:网络
Is there a way to write a qt library such that I can then use it (statically linked is fine) in a C application?

Is there a way to write a qt library such that I can then use it (statically linked is fine) in a C application? My C code is huge, old and will not convert to C++ without an inordinate amount of work. I say this as other similar questions seem to answer "just make your C code a Qt app". That's not an option. I hope I can write a qt library, and build it in a way that lets it be called from C (something alluded to in QLibrary documentation).

The symbol must be exported as a C function from the library for resolve() to work. This means that the function must be wrapped in an extern "C" block if the library is compiled with a C++ compiler. On Windows, this also requi开发者_JAVA百科res the use of a dllexport macro; see resolve() for the details of how this is done.

Can someone confirm/deny that I can do this, and let me know how much "qt" I can put in the library? I don't need a GUI but would like to use some of the SQL handling.

Cheers

Mike


You can put as much Qt in a library as you wish, including full UI capability. The rub is that since you want to access it from C code, you must provide your own access functions and your C functionality will be constrained to whatever level of access you provide.

You can even pass Qt object pointers between C and C++ but you'll need to cast them into something that C can compile -- either void * or preferably your own new type definition (such as C_QString *). To C code these pointers will be opaque, but they'll still be valid.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号